假设我有一个确定以 开头的文本块,我<?php
如何从原始文本中提取第一个 php 代码块?
示例文本:
<?php
echo "uh oh... \"; ?> in a \"; // string... ?>";
echo 'uh oh... \\\'; ?> in a /* string ?> */...';
// ?> nope not done
/*
?> still not done
*/
echo "this should be echoed too";
?>
this is even more text...
我想我需要使用 PHP 核心函数token_get_all
,但我不知道如何使用它来获取 PHP 块的大小以从原始字符串中删除。该函数显然知道它何时到达 PHP 块的末尾。我不能只将所有文本传递给 PHP,因为这在许多其他处理层中很深。
所以,最终结果应该让我在包含以下内容的 PHP 块末尾之外的字符串:(带有任何前面的空格)
this is even more text...
以及在不同字符串中解析出的 PHP 代码:
<?php
echo "uh oh... \"; ?> in a string...";
echo 'uh oh... \\\'; ?> in a string...';
// ?> nope not done
/*
?> still not done
*/
echo "this should be echoed too";
?>