在生成的php文件中包含动态PHP

时间:2021-02-04 09:57:57

I'm trying to generate php files that 'include' a template html file, as so:

我正在尝试生成'包含'模板html文件的php文件,如下所示:

$page = htmlspecialchars("Hello! <?php include("template.html"); ?>");

But, when I run it, I get:

但是,当我运行它时,我得到:

Parse error: syntax error, unexpected T_STRING in /home/oliver/web/postmanapp.com/public/core.php on line 15

解析错误:语法错误,第15行/home/oliver/web/postmanapp.com/public/core.php中的意外T_STRING

I'm pretty sure I need to escape the PHP code, just not sure how.

我很确定我需要逃避PHP代码,只是不确定如何。

3 个解决方案

#1


You have to escape any double quotes inside strings that are declared in double quotes. So this should work:

您必须转义以双引号声明的字符串内的任何双引号。所以这应该工作:

$code = "Hello! <?php include(\"template.html\"); ?>";

See PHP manual about strings.

有关字符串,请参阅PHP手册

And if you want to put that code into a file, you could use the file_put_contents function:

如果要将该代码放入文件中,可以使用file_put_contents函数:

file_put_contents('myscript.php', $code);

#2


The answer is a combination of some of the other suggestions, but neither of them would solve the problem, even combined!

答案是其他一些建议的组合,但它们都不会解决问题,甚至合并!

Problem 1. Unescaped Double Quotes

问题1.未转义的双引号

"Hello! <?php include("template.html"); ?>"

Here you have what looks like a single string. PHP sees the opening quote, and then it gets to the quote right before the word template and it says: oh, the string is done now. Then it sees template, which it thinks is a variable. Then it sees a dot and thinks it's a new concatenation operator. Then it sees the quote mark after html and it thinks it's a new string, which isn't allowed right after a variable like that.

在这里你看起来像一个字符串。 PHP看到开头的引用,然后它在单词模板之前得到引号,它说:哦,字符串现在就完成了。然后它看到模板,它认为它是一个变量。然后它看到一个点并认为它是一个新的连接运算符。然后它在html之后看到引号,它认为它是一个新的字符串,在这样的变量之后不允许。

So, you need either:

所以,你需要:

'Hello! <?php include("template.html"); ?>'

(Which is what I would recommend. Single quoted strings in PHP run slightly faster because it doesn't have to look inside the string for variables.)

(这是我推荐的。在PHP中单引号字符串运行得稍快,因为它不必查看字符串中的变量。)

or, as the other person suggested, using the escape character for the double quotes:

或者,正如另一个人建议的那样,使用双引号的转义字符:

"Hello! <?php include(\"template.html\"); ?>"

Problem 2. Wrong place for htmlspecialchars()

问题2. htmlspecialchars()的错误位置

But, you have another, worse problem. htmlspecialchars basically turns all HTML characters into their HTML character entities.

但是,你有另一个更糟糕的问题。 htmlspecialchars基本上将所有HTML字符转换为HTML字符实体。

In other words, < becomes &lt;.

换句话说, <变成<。< p>

So, what you're outputting into your PHP file will be this:

那么,您输出到PHP文件中的内容将是:

Hello! &lt;?php include("template.html"); ?&gt;

When you run that file, the PHP won't execute, because there isn't a valid open tag.

当您运行该文件时,PHP将不会执行,因为没有有效的打开标记。

Problem 3. This is not a good use of include

问题3.这不是一个很好的使用包括

I'm betting there's a WAY better way to solve your problem than to use PHP to write a PHP include tag that includes ANOTHER file.

我敢打赌,除了使用PHP编写包含ANOTHER文件的PHP包含标签之外,还有一种解决问题的方法。

An earlier user is right... file_put_contents() or file_get_contents() is probably something closer to what you want. But really, any time you're using PHP to write to PHP another file, there's probably a better way. I almost guarantee it.

较早的用户是正确的... file_put_contents()或file_get_contents()可能更接近您想要的东西。但实际上,无论何时使用PHP向PHP写入另一个文件,都可能有更好的方法。我差点保证。

#3


The problem is with the ?> which always ends a code block in php, leading to an unterminated string. All you need to do is split the end tag into two separate strings which can be concatenated:

问题在于?>总是在php中结束一个代码块,导致一个未终止的字符串。您需要做的就是将结束标记拆分为两个可以连接的单独字符串:

$page = htmlspecialchars("Hello! <?php include("template.html"); ?" . ">");

And, as the previous answer stated, you also need to escape the double quotes inside the string, or use single quotes:

并且,如前面的答案所述,您还需要转义字符串中的双引号,或使用单引号:

$page = htmlspecialchars("Hello! <?php include('template.html'); ?" . ">");

I'd also recomend getting into the habit of using single quotes whenever you're not using implictic variable substitution. I seem to remember reading that it's slightly faster, as the string doesn't need parsing for variable names.

当你不使用隐含变量替换时,我还建议养成使用单引号的习惯。我似乎记得读它稍微快一些,因为字符串不需要解析变量名。

#1


You have to escape any double quotes inside strings that are declared in double quotes. So this should work:

您必须转义以双引号声明的字符串内的任何双引号。所以这应该工作:

$code = "Hello! <?php include(\"template.html\"); ?>";

See PHP manual about strings.

有关字符串,请参阅PHP手册

And if you want to put that code into a file, you could use the file_put_contents function:

如果要将该代码放入文件中,可以使用file_put_contents函数:

file_put_contents('myscript.php', $code);

#2


The answer is a combination of some of the other suggestions, but neither of them would solve the problem, even combined!

答案是其他一些建议的组合,但它们都不会解决问题,甚至合并!

Problem 1. Unescaped Double Quotes

问题1.未转义的双引号

"Hello! <?php include("template.html"); ?>"

Here you have what looks like a single string. PHP sees the opening quote, and then it gets to the quote right before the word template and it says: oh, the string is done now. Then it sees template, which it thinks is a variable. Then it sees a dot and thinks it's a new concatenation operator. Then it sees the quote mark after html and it thinks it's a new string, which isn't allowed right after a variable like that.

在这里你看起来像一个字符串。 PHP看到开头的引用,然后它在单词模板之前得到引号,它说:哦,字符串现在就完成了。然后它看到模板,它认为它是一个变量。然后它看到一个点并认为它是一个新的连接运算符。然后它在html之后看到引号,它认为它是一个新的字符串,在这样的变量之后不允许。

So, you need either:

所以,你需要:

'Hello! <?php include("template.html"); ?>'

(Which is what I would recommend. Single quoted strings in PHP run slightly faster because it doesn't have to look inside the string for variables.)

(这是我推荐的。在PHP中单引号字符串运行得稍快,因为它不必查看字符串中的变量。)

or, as the other person suggested, using the escape character for the double quotes:

或者,正如另一个人建议的那样,使用双引号的转义字符:

"Hello! <?php include(\"template.html\"); ?>"

Problem 2. Wrong place for htmlspecialchars()

问题2. htmlspecialchars()的错误位置

But, you have another, worse problem. htmlspecialchars basically turns all HTML characters into their HTML character entities.

但是,你有另一个更糟糕的问题。 htmlspecialchars基本上将所有HTML字符转换为HTML字符实体。

In other words, < becomes &lt;.

换句话说, <变成<。< p>

So, what you're outputting into your PHP file will be this:

那么,您输出到PHP文件中的内容将是:

Hello! &lt;?php include("template.html"); ?&gt;

When you run that file, the PHP won't execute, because there isn't a valid open tag.

当您运行该文件时,PHP将不会执行,因为没有有效的打开标记。

Problem 3. This is not a good use of include

问题3.这不是一个很好的使用包括

I'm betting there's a WAY better way to solve your problem than to use PHP to write a PHP include tag that includes ANOTHER file.

我敢打赌,除了使用PHP编写包含ANOTHER文件的PHP包含标签之外,还有一种解决问题的方法。

An earlier user is right... file_put_contents() or file_get_contents() is probably something closer to what you want. But really, any time you're using PHP to write to PHP another file, there's probably a better way. I almost guarantee it.

较早的用户是正确的... file_put_contents()或file_get_contents()可能更接近您想要的东西。但实际上,无论何时使用PHP向PHP写入另一个文件,都可能有更好的方法。我差点保证。

#3


The problem is with the ?> which always ends a code block in php, leading to an unterminated string. All you need to do is split the end tag into two separate strings which can be concatenated:

问题在于?>总是在php中结束一个代码块,导致一个未终止的字符串。您需要做的就是将结束标记拆分为两个可以连接的单独字符串:

$page = htmlspecialchars("Hello! <?php include("template.html"); ?" . ">");

And, as the previous answer stated, you also need to escape the double quotes inside the string, or use single quotes:

并且,如前面的答案所述,您还需要转义字符串中的双引号,或使用单引号:

$page = htmlspecialchars("Hello! <?php include('template.html'); ?" . ">");

I'd also recomend getting into the habit of using single quotes whenever you're not using implictic variable substitution. I seem to remember reading that it's slightly faster, as the string doesn't need parsing for variable names.

当你不使用隐含变量替换时,我还建议养成使用单引号的习惯。我似乎记得读它稍微快一些,因为字符串不需要解析变量名。