Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带有preg_replace_回调的正则表达式,用于亵渎过滤器_Php_Regex_Preg Replace_Preg Replace Callback - Fatal编程技术网

Php 带有preg_replace_回调的正则表达式,用于亵渎过滤器

Php 带有preg_replace_回调的正则表达式,用于亵渎过滤器,php,regex,preg-replace,preg-replace-callback,Php,Regex,Preg Replace,Preg Replace Callback,我有一个工作函数,它获取一系列坏单词,然后用星号替换坏单词 当我升级到PHP7时,我不得不使用preg\u replace\u回调,因为preg\u replacee修饰符被折旧 我就是这样使用它的: function filterwords($text){ $filterWords = array("dummy"); $filterCount = sizeof($filterWords); for($i=0; $i<$filterCount

我有一个工作函数,它获取一系列坏单词,然后用星号替换坏单词

当我升级到PHP7时,我不得不使用
preg\u replace\u回调
,因为
preg\u replace
e
修饰符被折旧

我就是这样使用它的:

function filterwords($text){

       $filterWords = array("dummy");
       $filterCount = sizeof($filterWords);

        for($i=0; $i<$filterCount; $i++){
            $text = preg_replace('/\b'.$filterWords[$i].'\b/ie',"str_repeat('*',strlen('$0'))",$text);
        }

 return $text;
}
函数筛选词($text){
$filterWords=数组(“虚拟”);
$filterCount=sizeof($FilterWord);

对于($i=0;$i在
preg\u replace
中使用的像
preg\u replace
这样的反向引用在
preg\u replace\u callback
中没有任何意义。您将匹配项作为
$matches
传递到函数中,但您正在检查
strlen('$0')
,它只是一个2个字符串
$0
,因此您将得到2个
*/code>

使用
$matches
和backreference的编号。正如您习惯的那样,
0
是完全匹配:

return str_repeat('*', strlen($matches[0]));

我回答说要帮助你,但实际上没有研究工作,正如页面所显示和解释的那样。
return str_repeat('*', strlen($matches[0]));