我在PHP中使用快速表单创建的小框架,当我手动编写代码时所有工作都正确但是当我想要包含来自txt文件的代码时我遇到了问题(我为那些只写字符串的用户提供了这个选项txt),查看我的示例以便更好地理解:
$my_test_variable = "How are you?";
$show_this = "|Example 1|Example 2|$my_test_variable|";
$a = explode ("|",$show_this);
echo "First example: $a[1] Second example: $a[2] Variable to show: $a[3]";
// This example output: First example: Example 1 Second example: Example 2 Variable to show: How are you?
第二个例子:
//test.txt -> |Example 1|Example 2|$my_test_variable|
$show_this = file_get_contents ("test.txt");
$a = explode ("|",$show_this);
echo "First example: $a[1] Second example: $a[2] Variable to show: $a[3]";
// This example output: First example: Example 1 Second example: Example 2 Variable to show: $my_test_variable
// This is problematic because I want that my $my_test_variable show "How are you";
你是否知道我如何将$变量放入txt文件?当file_get_contents威胁这个变量而不仅仅是一个字符串?
答案 0 :(得分:3)
您可以尝试:
$a = explode ("|",$show_this);
foreach ($a as &$val) {
if (strpos($val, '$') === 0) {
$val = ${substr($val, 1)};
}
}
echo "First example: $a[1] Second example: $a[2] Variable to show: $a[3]";