简体   繁体   English

此表达式的preg_replace_callback

[英]preg_replace_callback for this expression

I have a preg_replace code in my php file:- 我的php文件中有一个preg_replace代码:-

preg_replace('/<([^>]+)>/es', "'<'.sanitize('\\1',5).'>'",strip_tags($var, $allowable_tags))

I want to convert it into preg_replace_callback. 我想将其转换为preg_replace_callback。

preg_replace_callback('/<([^>]+)>/s', function($m){return '<'.sanitize($m,5).'>';},strip_tags($var, $allowable_tags))

But it is not giving the same results. 但是它并没有给出相同的结果。 Can someone help? 有人可以帮忙吗?

But it is not giving the same results. 但是它并没有给出相同的结果。

You use '\\\\1' in the preg_replace replacement string. 您在preg_replace替换字符串中使用'\\\\1' The equivalent in the preg_replace_callback function($m) body were not $m , but $m[1] . preg_replace_callback function($m)主体中的等效项不是$m ,而是$m[1]

You can also use clean interface of T-Regx library 您也可以使用T-Regx库的干净接口

pattern('<([^>]+)>', 's')->replace($input)->callback(function (Match $m) {
    return '<' . sanitize($m, 5) . '>';
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM