- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 HTML 字符串 与 混合PHP 代码 .所以我只想对可能的代码进行评估并将其替换回去。我的想法是这样的:
$html='Hi it <b>PAPION</b>. Now timestamp is <?php echo time(); ?>. have a good time.';
$html = preg_replace_callback('/(<\?php)(.*?)(\?>)/ims',function($matches){
try {
ob_start();
eval($matches[2]);
return ob_get_clean();
} catch(Exception $e) {
return "";
}
}, $html);
$html='Hi it <b>PAPION</b>. Now timestamp is <?php echo xxtime(); ?>. have a good time.';
$html = preg_replace_callback('/(<\?php)(.*?)(\?>)/ims',function($matches){
try {
ob_start();
eval($matches[2]);
return ob_get_clean();
} catch(Exception $e) {
return "";
}
}, $html);
最佳答案
您在 catch 中将 $html 设置为 ""。必须更改您的代码,如下所示:
$html='Hi it <b>PAPION</b>. Now timestamp is <?php echo xxtime(); ?>. have a good time.';
preg_match('/(<\?php)(.*?)(\?>)/ims', $html, $matches);
$html = str_replace($matches[2], getEvalOutput($matches[2]), html);
echo "html is ".$html;
function getEvalOutput($matche){
try {
ob_start();
eval($matche);
return ob_get_clean();
} catch(Exception $e) {
return " ERROR happened";
}
}
<?php
$phpCode = "
<?php
set_error_handler('myErrorHandler');
register_shutdown_function('fatalErrorShutdownHandler');
function myErrorHandler(\$code, \$message, \$file, \$line) {
echo \$message;
}
function fatalErrorShutdownHandler()
{
\$last_error = error_get_last();
if (\$last_error['type'] === E_ERROR) {
// fatal error
myErrorHandler(E_ERROR, \$last_error['message'], \$last_error['file'], \$last_error['line']);
}
}
\$html='Hi it <b>PAPION</b>. Now timestamp is <?php echo xxtime(); ?>. have a good time.';
\$html = preg_replace_callback('/(<\?php)(.*?)(\?>)/ims',function(\$matches){
try {
ob_start();
eval(\$matches[2]);
return ob_get_clean();
} catch(Exception \$e) {
}
}, \$html);
echo \$html;";
file_put_contents('a.php', $phpCode);
$x = exec( 'php a.php');
echo $x;
/* delete the file */
unlink('a.php');
关于php - 如何处理 preg-replace-callback 中的 eval 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58242171/
如何用 preg_replace 替换格式为“Fri Mar 23 15:21:08 2012”的日期/时间?这种格式的日期在我的文本中出现了几次,我需要将其替换为当前时间/日期。 谢谢, 克里斯 最
如何使用 preg replace 替换单个问号。 最佳答案 preg_replace('/\?/', 'replacement', $original, 1) 关于php: preg 替换单个问号?
如何预匹配所有字符 字母、数字、特殊字符 f.e: ²,▲,ƒ ² ‰ © £ ± ß ° 1/2 ¿ « » ™...等等...??? 和其他特殊字符??? 最佳答案 除换行符(\r 或\n)之外的
我有一个 preg match 语句,它检查匹配,但我想知道如何计算匹配。任何建议表示赞赏。 $message='[tag] [tag]'; preg_match('/\[tag]\b/i',$mes
如何对 null(空)字符串进行 preg_match? 我需要类似的东西: /([0-9]|[NULL STRING])/ 最佳答案 您可以使用 ^ 来匹配行首,使用 $ 来匹配行尾,因此正则表达式
Inspection and treatment will be conducted to areas prone to Mouse infestation by our Service Specia
我有像这样的库中的 mysql 函数 PREG_CAPTURE('/id\=\'(.*)\'/i', xml, 1, 1) ,并且我 try catch 元素从这个日志 "xml":id='t6e8u
如何使用 preg_replace 将多个下划线替换为一个下划线? 最佳答案 + operator (quantifier)匹配最后一个字符的多个实例(、字符类或捕获组或反向引用)。 $string
我正在使用预匹配来验证密码: (preg_match("/^.*(?=.{5,}).*$/", $password) 它也接受特殊字符。 但出于某种原因,我需要修改它应该接受 仅字母数字 至少 5 个
我创建了一个模式来匹配 a 标签之间的 3 个数字(例如:333)的字符串: @(((.?[^()].?))*)([0-9]{3})(((.*?)?))@i 我如何反转上面的模式以获得不在 a 标记之
在http://gskinner.com/RegExr/下面的正则表达式工作但不是在 PHP 中 (?>AB/HDR)(?:[^])*?(?=AB/HDR|RUN DATE|\z) 在 PHP 中是这
在php中preg_match()函数是用来执行正则表达式的一个常用的函数。正则表达式几乎在所有编程语言里面都会用到,本实例介绍php中正则表达式preg_match函数的应用。 preg_mat
我在 d:/www/web12/index.php 中有一个 index.php 里面有一些 anchor 标签,例如 我想要 preg_replace 它们到 $url = preg_rep
我有这个代码: $1', $test); echo $test; ?> 我想做:$array[] = $1 请问有人有什么建议吗? 最佳答案 如果你使用 PHP ≥ 5.3.0 你可以使用 anony
我有一行我必须替换下面的行 /myhome/ishere/where/are.you.ha 这里我有一个实时正则表达式 preg_match('/(.+\/)[^.]+(.+\.ha)/', $inp
我有一行我必须替换下面的行 /myhome/ishere/where/are.you.ha 这里我有一个实时正则表达式 preg_match('/(.+\/)[^.]+(.+\.ha)/', $inp
您好,我使用 preg_replace 将非字母数字数据替换为“”。 1) SELECT PREG_REPLACE('[^A-Za-z0-9]','',CATALOGUEREFERENCE)
我刚刚升级到 PHP 7,并一直在努力解决与已弃用函数相关的错误,并取得了很大成功。 遗憾的是,我在修复“在交互式可折叠 javascript 中查看 php 数组”代码的新 preg 替换方法时遇到
好吧,我知道有几个类似的问题,但在这个特定案例中找不到任何问题。 我使用了一段代码并根据需要对其进行了调整,但现在我在其中发现了一个我无法纠正的错误。 代码: $tag = 'namespace';
我有这段代码: function queryString(){ //matches up to 10 digits in page number $query_string = ere
我是一名优秀的程序员,十分优秀!