识别并执行字符串上的 php 代码

发布于 2024-11-08 21:58:04 字数 606 浏览 0 评论 0原文

我想知道是否可以在字符串中执行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦在深巷 2024-11-15 21:58:04
$string = ($test === TRUE) ? 'lala' : 'falala';

有很多方法可以做你想做的事情(如果我正确地阅读你写的内容)。上面是三元的。如果条件计算结果为真,则 $string 将设置为“lala”,否则设置为“falala”。

如果您实际上是在询问您写的内容,请使用 eval() 函数。它接受一个传递的字符串并执行它,就好像它是 php 代码一样。请勿包含 标记。

function dropAllTables() {
    // drop all tables in db
}

$string = 'dropAllTables();';

eval($string); // will execute the dropAllTables() function

[编辑]

您可以使用以下正则表达式来查找所有 php 代码:

preg_match_all('/(<\?php )(.+?)( \?>)/', $string, $php_code, PREG_OFFSET_CAPTURE);

$php_code 将是一个数组,其中 $php_code[0] 将返回与代码 + 匹配的所有匹配项的数组; 标签。 $php_code[2] 将是一个仅包含要执行的代码的数组。

所以,

$string = "array has <?php count(array('lala')); ?> 1 member <?php count(array('falala')); ?>";
preg_match_all('/(<\?php )(.+?)( \?>)/', $string, $php_code, PREG_OFFSET_CAPTURE);
echo $php_code[0][0][0]; // <?php count(array('lala')); ?>
echo $php_code[2][0][0]; // count(array('lala'));

这应该对你想做的事情有帮助。

$string = ($test === TRUE) ? 'lala' : 'falala';

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.

function dropAllTables() {
    // drop all tables in db
}

$string = 'dropAllTables();';

eval($string); // will execute the dropAllTables() function

[edit]

You can use the following regular expression to find all the php code:

preg_match_all('/(<\?php )(.+?)( \?>)/', $string, $php_code, PREG_OFFSET_CAPTURE);

$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,

$string = "array has <?php count(array('lala')); ?> 1 member <?php count(array('falala')); ?>";
preg_match_all('/(<\?php )(.+?)( \?>)/', $string, $php_code, PREG_OFFSET_CAPTURE);
echo $php_code[0][0][0]; // <?php count(array('lala')); ?>
echo $php_code[2][0][0]; // count(array('lala'));

This should be helpful for what you want to do.

栀梦 2024-11-15 21:58:04

看来您正在尝试连接。使用连接运算符“.”

$string = "if i say " . $lala . " I wanna get " . $dada;

或者

$string = "if i say {$lala} I wanna get {$dada}.";

这就是我得到的,因为你的字符串看起来是一个 php 变量。

编辑:
当您想告诉 PHP 解释器这些括号中的代码应该被解释为 PHP 时,可以使用 。在这些 PHP 括号内工作时,您不需要再次包含它们。正如您所做的那样:

// You create a string:
$myString = "This is my string.";

// You decide you want to add something to it.
$myString .= getMyNameFunction(); // not $myString .= <?php getMyNameFunction() ?>;

创建字符串,然后将 getMyNameFunction() 的结果附加到它。现在,如果您在页面顶部声明了 $myString 变量,并且想要稍后使用它,您可以这样做:

<span id="myString"><?php echo $myString; ?></span>

这将告诉解释器在标签之间添加 $myString 变量的内容。

Looks like you are trying to concatenate. Use the concatenation operator "."

$string = "if i say " . $lala . " I wanna get " . $dada;

or

$string = "if i say {$lala} I wanna get {$dada}.";

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:

// You create a string:
$myString = "This is my string.";

// You decide you want to add something to it.
$myString .= getMyNameFunction(); // not $myString .= <?php getMyNameFunction() ?>;

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:

<span id="myString"><?php echo $myString; ?></span>

This would tell the interpreter to add the contents of the $myString variable between the tags.

┈┾☆殇 2024-11-15 21:58:04

使用 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 a T_OPEN_TAG token, start copying from there, look for a T_CLOSE_TAG token and stop there. The string between the token next to T_OPEN_TAG and until the token right before T_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.

哽咽笑 2024-11-15 21:58:04

我会考虑不将 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.

十级心震 2024-11-15 21:58:04

您可以使用 eval() 函数。

You could use the eval() function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文