$string ='likeyou小白喜欢小黑爱着的大黄'; //要过滤的内容
$list = ['小明', '小红', '大白', '小白', '小黑', 'me', 'you'];
function sensitive($string,$list){
$newStr = implode('|',$list);
$pattern ="/$newStr/i";
if(preg_match_all($pattern,$string,$matches)){
$newArr = $matches[0];//匹配成功数组
$newReplace = array_combine($newArr,array_fill(0,count($newArr),'*'));
$newStr = strtr($string,$newReplace);
echo '<pre>';
var_dump($newReplace);
或者:
$newArr = $matches[0];//匹配成功数组
$pattern = implode('|',$newArr);
$newStr = preg_replace("/$pattern/i",'*',$string);
echo '<pre>';
var_dump($newStr);
}
}
sensitive($string,$list);
exit;
$string ='likeyou小白喜欢小黑爱着的大黄'; //要过滤的内容
$list = ['小明', '小红', '大白', '小白', '小黑', 'me', 'you'];
function sensitive($string,$list){
$newStr = implode('|',$list);
$pattern ="/$newStr/i";
if(preg_match_all($pattern,$string,$matches)){
$newArr = $matches[0];//匹配成功数组
$a = [];
$b='';
foreach($newArr as $v){
$a[]=str_pad($b,mb_strlen($v,'UTF-8'),'*');
}
$newReplace = array_combine($newArr,$a);
$newStr = strtr($string,$newReplace);
echo '<pre>';
var_dump($newStr);
}
}
sensitive($string,$list);
exit;