Php 检测字符串中的禁止字
我有一个检查坏单词的功能,但它不能按我所希望的方式工作。例如,test是一个脏话,如果我说“testing”,那么函数将把它算作脏话。我该如何解决这个问题,使它不会这样做 这是我的密码:Php 检测字符串中的禁止字,php,strpos,Php,Strpos,我有一个检查坏单词的功能,但它不能按我所希望的方式工作。例如,test是一个脏话,如果我说“testing”,那么函数将把它算作脏话。我该如何解决这个问题,使它不会这样做 这是我的密码: function censor($message) { $badwords = $this->censor; //array with the cuss words. $message = @ereg_replace('[^A-Za-z0-9 ]','',strto
function censor($message) {
$badwords = $this->censor; //array with the cuss words.
$message = @ereg_replace('[^A-Za-z0-9 ]','',strtolower(' '.$message.' '));
foreach($badwords as $bad) {
$bad = trim($bad);
if(strpos($message.' ', $bad.' ')!==false) {
if(strlen($bad)>=2) {
return true;
}
}
}
}
首先,从PHP5.3.0开始,它就被弃用了
现在,回答您的问题:您可以使用\b
作为单词边界
简单地说:\b
允许您使用
格式为\bword\b
的正则表达式
有关更多详细信息,请参阅
您甚至可以使用下面的代码,我从PHP文档的示例2中复制了这些代码:
$string = 'The quickest brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[0] = '/ quick /';
$patterns[1] = '/ brown /';
$patterns[2] = '/ fox /';
echo preg_replace($patterns, ' *** ', $string);
Output: The quickest *** *** jumped over the lazy dog.
在正则表达式中使用\b(在开始和结束处)。它与单词边界相匹配。
ereg\ug
函数因某种原因被弃用。使用@
抑制通知并不会使其变得更好。切换到preg\u
函数。@HappyTimeGopher我在哪里添加它?你能告诉我我在哪行添加它吗?