2016-09-25 49 views
0

我有一个函数:PHP的file_get_contents与变量

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

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

我想所有的sentenses保存在功能到另一个文件,因为它们的长度太长,把它放在我的功能。

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

inc_file.html:

Hello $a, how are you today? 

但它并没有返回如我所料。变量$ a不会更改为John。 请帮帮我! 谢谢

+0

不,我不希望保存文件,某些功能。我手动创建html文件。仅用于阅读和显示HTML文件内容的PHP代码 –

回答

0

你可以包括.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? 
+0

它没有工作。我认为是因为函数“file_get_contents”不能传递变量。但是,如果我使用“包含”,而不是我的变量$ str –

+0

当我测试它工作 –

0

$str = file_get_contents("inc_file.html");线仅获得文件内容。它未评估此内容和不会取代变量与它的值。

假设,会发生什么,如果文件将是巨大的会有一些关于PHP变量的特定文本?

他们都应该被一些值取代?我想不是。所以,你只是有一个像$a这样的符号的字符串。

如果你有一个字符串替换的东西 - 用str_replace,简单的代码是:

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

你有什么是好的,你可以只使用一个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')); 
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

+0

没有返回值没关系。但是我的func中有很多变量。我之前在代码中定义了所有这些变量。我必须重写所有的str_replace函数?我真的希望它能自动检测存在的变量而不需要替换。 –

0

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

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) ;