Dai Chong's blog

替换文字为星号的功能经常会遇到,比如关键词过滤,聊天内容过滤等,话不多说直接上代码!

[实现代码]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$text = "苏菲的世界安防科技按时付款链接萨达哈垃圾卡刷卡费SAP发生破房间爱上;靠谱积分卡普斯安徽交付恐怕是电话费破ID卡手IP卡";
// 回调函数
function next_year($matches)
{
$len = mb_strlen($matches[0],'UTF8');
$res = '';
for($i=1;$i<=$len;$i++){
$res.='*';
}
return $res;
}
$arr = array('链接','卡');
$preg_str = implode('|',$arr);
echo preg_replace_callback("/($preg_str)/","next_year",$text);

 评论