我是PHP新手
我有一个这样的数组
$suspiciousList = array(
array ("word" => "badword1", "score" => 400, "type" => 1),
array ("word" => "badword2", "score" => 250, "type" => 1),
array ("word" => "badword3", "score" => 400, "type" => 1),
array ("word" => "badword4", "score" => 400, "type" => 1));当用户输入像(badw ord1,b adword2等)这样的空格,或者用户可能像(b A D W or R D 1)这样输入单词时,我遇到了问题
如何从数组(字典)中检测或搜索组合?
我的想法是让每个单词都变成一个被空格分割的数组。
$this->suspiciousPart[] = $word;
我正在编写以下函数
public function deepDetect2() {
for($i=0;$i<sizeof($this->suspiciousPart);$i++) {
$word = "";
for($j=$i;$j<sizeof($this->suspiciousPart);$j++) {
$word .= $this->suspiciousPart[$j];
//var_dump($word);
if(strpos(in_array($word, $this->suspiciousList), $word) !== false) {
if($this->detect($word) == true) {
$i++;
} else {
$j++;
}
} else {
$i++;
}
}
}
}有没有人有其他的想法呢?
谢谢
发布于 2011-06-19 17:27:50
无论如何,您可以去掉空格字符并使用(mb_)substr_count(),但这会导致误报。
发布于 2011-06-19 17:44:53
正如Jirka Helmich建议的那样,您可以删除空格(可能还有其他特殊字符),然后搜索字符串以从数组中查找单词。
public function searchForBadWords($strippedText) {
foreach($suspiciousList as $suspiciousPart) {
$count = substr_count($strippedText, $suspiciousPart['word']);
//you can use str_replace here or something, it depends what you want to achive
}
}问题是,如果你有像blablabad wordblabla这样的单词,你去掉了正常单词的空格,可能会变成坏词blablabadwordblabla (明白我的意思吗?) :D
干杯
编辑:所以艾哈迈德,我看到你只是通过在开头/结尾(快捷方式)上的“”来识别它们的单词。也许您应该尝试实现这两种方法,您的方法使用单个单词,上面的方法使用子字符串搜索。这还取决于您对性能的关心程度。也许你应该试着做一些研究或做点什么,看看它有多有效?
发布于 2011-06-19 18:46:58
(word1|word2|word3)使用一个包含所有关键字的正则表达式来分隔
https://stackoverflow.com/questions/6401558
复制相似问题