【问题标题】:PHP file_get_contents with variablesPHP file_get_contents 与变量
【发布时间】:2017-03-23 02:56:40
【问题描述】:

我有一个函数:

myfunc () {
   $a = "John";
   $str = "Hello $a, how are you today?"
   return $str;
}

echo myfunc(); //Hello John, how are you today?

我想将该函数中的所有句子保存到另一个文件中,因为它们的长度太长,无法放入我的函数中。

myfunc () {
  $a = "John";
  $str = file_get_contents("inc_file.html");
  return $str;
}

inc_file.html:

Hello $a, how are you today?

但它并没有像我预期的那样返回。变量 $a 未更改为 John。 请帮我 ! 谢谢

【问题讨论】:

  • 不,我不想用某些功能保存文件。我手动创建 html 文件。 php代码仅用于读取和显示html文件中的内容

标签: php variables file-get-contents


【解决方案1】:

正如函数名所暗示的那样,file_get_contents() 所做的只是返回文件的内容。它不会解析 "s 中的字符串所发生的内容。

在返回的文件内容上使用str_replace() 似乎可以达到您想要的效果。

$str = file_get_contents("inc_file.html");
$str = str_replace('$a', 'John', $str);
return $str;

如果您想替换多个“变量”,您可以将数组传递给str_replace,例如

$search = [$a, $b];
$replace = ['John', 'Brian'];

$str = file_get_contents("inc_file.html");
$str = str_replace($search, $replace, $str);
return $str;

http://php.net/manual/en/function.str-replace.php

【讨论】:

  • 没关系。但是我的函数中有很多变量。我之前在代码中定义了所有这些变量。我必须在 str_replace 函数中重写所有这些?我真的希望它自动检测存在的变量而不替换。
【解决方案2】:

$str = file_get_contents("inc_file.html"); 行只是获取文件内容。它不评估此内容,并且不替换 vars与其'值。

假设,如果文件很大或者会有一些关于 php 变量的特定文本会发生什么?

它们都应该用一些值替换吗?我想不会。所以,你只有一个带有$a 之类的符号的字符串。

如果您必须替换字符串中的某些内容 - 使用 str_replace,简单的代码是:

function myfunc () {
    $a = "John";
    $str = file_get_contents("inc_file.html");
    return str_replace('$a', $a, $str);
}

【讨论】:

    【解决方案3】:

    您所拥有的一切都很好,您可以使用preg_replace_callback() 或在本例中为str_replace() 来做您想做的事情。只需做一些调整:

    HTML:

    "Hello ~val~, how are you today?"
    

    PHP

    function myfunc ($a)
        {
            $str = str_replace('~val~',$a,file_get_contents("inc_file.html"));
            return $str;
        }
    
    echo myfunc('John');
    

    简单的preg_replace_callback() 例子:

    function myfunc ($string,$a)
        {
    
            $str = preg_replace_callback('/(~[^~]{1,}~)/',function($v) use (&$a)
                {
                    return array_shift($a);
                },$string);
    
            return $str;
        }
    
    $string = "hello my name is ~name~. I come from ~city~";
    echo myfunc($string,array('John','San Francisco'));
    

    【讨论】:

      【解决方案4】:

      您可以包含 .php 文件,但 .html 不起作用,因为您包含 html 而不是 php 代码。如果您像这样编写代码和文件:

      <?php
      function myfunc() 
      {
        $a = "John";
        $str = file_get_contents("inc_file.php");
        return $str;
      }
      ?>
      

      inc_file.php:

      Hello <?php echo $a; ?>, how are you today?
      

      【讨论】:

      • 它没有用。我认为是因为函数“file_get_contents”没有传递变量。但如果我改用“include”,它不会为我的变量 $str 返回值
      【解决方案5】:

      这是一个从加载的文件字符串中替换任何匹配变量的方法。这是来自下面链接的代码的修改版本。我对其进行了修改以检查字符串类型。它假设变量是全局的。

      How can I output this dynamic data without eval?

      function interpolate( $string ){
      foreach ($GLOBALS as $name => $value){
          // avoid array to string and object conversion errors
        if(is_string($value)){
          $string = str_replace( '$'.$name, $value, $string );
        }
      }
      $string = preg_replace( '/[$]\\w+/', '', $string );
      return $string;
      }
      $emailTemplate = file_get_contents('inc_file.html');
      $emailTemplate = interpolate($emailTemplate);
      echo $emailTemplate;
      

      我在寻找一种不使用 eval() 的方法后发现了这一点,如下所示:

      $emailTemplate = file_get_contents('inc_file.html');
      $emailTemplate = htmlentities($emailTemplate);
      eval("\$emailTemplate = \"$emailTemplate\";");
      echo html_entity_decode($emailTemplate) ;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-17
        • 1970-01-01
        • 2012-06-19
        • 1970-01-01
        • 2012-03-25
        • 2013-05-10
        • 1970-01-01
        相关资源
        最近更新 更多