识别并执行字符串上的 php 代码
我想知道是否可以在字符串中执行php代码。我的意思是,如果我有:
$string = If i say <?php echo 'lala';?> I wanna get "<?php echo 'dada'; ?>";
有人知道怎么做吗?
[编辑] 看起来没人理解。我想像
$string = If i say <?php count(array('lala'));?>
在数据库中一样保存一个字符串,然后渲染它。我可以使用
function render_php($string){
ob_start();
eval('?>' . $string);
$string = ob_get_contents();
ob_end_clean();
return $string;
}
问题是我没有将 php 代码重新识别为“”(引号),例如
I say "<?php echo 'dada'; ?>"
I would like to know if it's possible to execute the php code in a string. I mean if I have:
$string = If i say <?php echo 'lala';?> I wanna get "<?php echo 'dada'; ?>";
Does anybody knows how?
[EDIT] It looks like nobody understood. I wanna save a string like
$string = If i say <?php count(array('lala'));?>
in a database and then render it. I can do it using
function render_php($string){
ob_start();
eval('?>' . $string);
$string = ob_get_contents();
ob_end_clean();
return $string;
}
The problem is that I does not reconize php code into "" (quotes) like
I say "<?php echo 'dada'; ?>"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有很多方法可以做你想做的事情(如果我正确地阅读你写的内容)。上面是三元的。如果条件计算结果为真,则 $string 将设置为“lala”,否则设置为“falala”。
如果您实际上是在询问您写的内容,请使用 eval() 函数。它接受一个传递的字符串并执行它,就好像它是 php 代码一样。请勿包含
标记。
[编辑]
您可以使用以下正则表达式来查找所有 php 代码:
$php_code 将是一个数组,其中 $php_code[0] 将返回与代码 +
匹配的所有匹配项的数组;
标签。 $php_code[2] 将是一个仅包含要执行的代码的数组。所以,
这应该对你想做的事情有帮助。
There are lots of ways to do what it looks like you're trying to do (if I'm reading what you wrote correctly). The above is a ternary. If the condition evaluates to true then $string will be set to 'lala' else set to 'falala'.
If you're literally asking what you wrote, then use the eval() function. It takes a passed string and executes it as if it were php code. Don't include the
<?php ?>
tags.[edit]
You can use the following regular expression to find all the php code:
$php_code will be an array where $php_code[0] will return an array of all the matches with the code +
<?php ?>
tags. $php_code[2] will be an array with just the code to execute.So,
This should be helpful for what you want to do.
看来您正在尝试连接。使用连接运算符“.”
或者
这就是我得到的,因为你的字符串看起来是一个 php 变量。
编辑:
当您想告诉 PHP 解释器这些括号中的代码应该被解释为 PHP 时,可以使用
。在这些 PHP 括号内工作时,您不需要再次包含它们。正如您所做的那样:
创建字符串,然后将 getMyNameFunction() 的结果附加到它。现在,如果您在页面顶部声明了 $myString 变量,并且想要稍后使用它,您可以这样做:
这将告诉解释器在标签之间添加 $myString 变量的内容。
Looks like you are trying to concatenate. Use the concatenation operator "."
or
That is what I get since your string looks to be a php variable.
EDIT:
<?php ?>
is used when you want to tell the PHP interpreter that the code in those brackets should be interpreted as PHP. When working within those PHP brackets you do not need to include them again. So as you would just do this:The string is created, then the results of getMyNameFunction() are appended to it. Now if you declared the $myString variable at the top of your page, and wanted to use it later you would do this:
This would tell the interpreter to add the contents of the $myString variable between the tags.
使用
token_get_all()
在字符串上,然后查找T_OPEN_TAG
标记,从那里开始复制,查找T_CLOSE_TAG
标记并在那里停止。T_OPEN_TAG
旁边的标记和T_CLOSE_TAG
之前的标记之间的字符串是您的 PHP 代码。这很快并且不会失败,因为它使用 PHP 的标记生成器来解析字符串。您总是会在字符串中找到 PHP 代码位,即使该字符串包含注释或其他可能包含
?>
的字符串或任何其他会混淆正则表达式或手写字符串的相关子字符串。 ,缓慢,纯PHP解析器。Use
token_get_all()
on the string, then look for aT_OPEN_TAG
token, start copying from there, look for aT_CLOSE_TAG
token and stop there. The string between the token next toT_OPEN_TAG
and until the token right beforeT_CLOSE_TAG
is your PHP code.This is fast and cannot fail, since it uses PHP's tokenizer to parse the string. You will always find the bits of PHP code inside the string, even if the string contains comments or other strings which might contain
?>
or any other related substrings that will confuse regular expressions or a hand-written, slow, pure PHP parser.我会考虑不将 PHP 代码块存储在数据库中并使用 eval 对其进行评估。通常有更好的解决方案。了解设计模式、OOP、多态性。
I would consider not storing your PHP code blocks in a database and evaluating them using eval. There is usually a better solution. Read about Design Pattern, OOP, Polymorphism.
您可以使用 eval() 函数。
You could use the eval() function.