PHP文件\u获取\u包含变量的内容
我有一个功能:PHP文件\u获取\u包含变量的内容,php,variables,file-get-contents,Php,Variables,File Get Contents,我有一个功能: 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
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。
请帮帮我!
谢谢您可以包含.php文件,但是wit.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?
你好,今天好吗?
您可以包含.php文件,但wit.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?
你好,今天好吗?
Line$str=file\u get\u contents(“inc\u file.html”)代码>仅获取文件内容。它不评估此内容,也不使用其值替换变量
假设,如果文件很大,并且有一些关于php变量的特定文本,会发生什么
它们都应该替换为一些值?我想不是。因此,您只需要一个带有符号的字符串,如$a
如果您必须替换字符串中的某些内容,请使用str\u replace
,简单的代码是:
function myfunc () {
$a = "John";
$str = file_get_contents("inc_file.html");
return str_replace('$a', $a, $str);
}
行$str=file\u get\u contents(“inc\u file.html”)代码>仅获取文件内容。它不评估此内容,也不使用其值替换变量
假设,如果文件很大,并且有一些关于php变量的特定文本,会发生什么
它们都应该替换为一些值?我想不是。因此,您只需要一个带有符号的字符串,如$a
如果您必须替换字符串中的某些内容,请使用str\u replace
,简单的代码是:
function myfunc () {
$a = "John";
$str = file_get_contents("inc_file.html");
return str_replace('$a', $a, $str);
}
您所拥有的一切都很好,您可以使用preg\u replace\u callback()
或在本例中使用str\u 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\u replace\u 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'));
您所拥有的一切都很好,您可以使用preg\u replace\u callback()
或在本例中使用str\u 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\u replace\u 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'));
正如函数名所示,file\u get\u contents()
只返回文件的内容。它不会解析“
s中字符串的内容
对返回的文件内容使用str\u replace()
,似乎可以达到您想要的效果
$str = file_get_contents("inc_file.html");
$str = str_replace('$a', 'John', $str);
return $str;
例如,如果要替换多个“变量”,可以将数组传递给str\u replace
$search = [$a, $b];
$replace = ['John', 'Brian'];
$str = file_get_contents("inc_file.html");
$str = str_replace($search, $replace, $str);
return $str;
请参见file\u get\u contents()
所做的一切就是返回文件的内容,正如函数名所示。它不会解析内容,而在“
中的字符串会发生这种情况
对返回的文件内容使用str\u replace()
,似乎可以达到您想要的效果
$str = file_get_contents("inc_file.html");
$str = str_replace('$a', 'John', $str);
return $str;
例如,如果要替换多个“变量”,可以将数组传递给str\u replace
$search = [$a, $b];
$replace = ['John', 'Brian'];
$str = file_get_contents("inc_file.html");
$str = str_replace($search, $replace, $str);
return $str;
请参见这里有一个方法,用于替换加载文件字符串中的任何匹配变量。这是下面链接中代码的修改版本。我修改它来检查字符串类型。它假设变量是全局变量
我在搜索一种不使用eval()的方法后发现了这一点,如下所示:
$emailTemplate = file_get_contents('inc_file.html');
$emailTemplate = htmlentities($emailTemplate);
eval("\$emailTemplate = \"$emailTemplate\";");
echo html_entity_decode($emailTemplate) ;
这里有一个方法可以替换加载文件字符串中的任何匹配变量。这是下面链接中代码的修改版本。我修改它来检查字符串类型。它假设变量是全局变量
我在搜索一种不使用eval()的方法后发现了这一点,如下所示:
$emailTemplate = file_get_contents('inc_file.html');
$emailTemplate = htmlentities($emailTemplate);
eval("\$emailTemplate = \"$emailTemplate\";");
echo html_entity_decode($emailTemplate) ;
不,我不想用一些函数保存文件。我手动创建html文件。php代码仅用于读取和显示html文件中的内容否,我不想使用某些函数保存文件。我手动创建html文件。php代码只用于读取和显示html文件中的内容,但不起作用。我认为是因为函数“file\u get\u contents”没有传递变量。但如果我改用“include”,它不会返回变量$strIt的值,因为它不起作用。我认为是因为函数“file\u get\u contents”没有传递变量。但如果我使用“include”,它不会返回变量$strIt的值。但是我的函数中有很多变量。我以前在代码中定义了所有这些变量。我必须在str_replace函数中重写它们?我真的希望它能自动检测存在的变量而不需要替换。这很好。但是我的函数中有很多变量。我以前在代码中定义了所有这些变量。我必须在str_replace函数中重写它们?我真的希望它能够自动检测存在的变量,而无需替换。