【发布时间】:2014-07-04 13:57:31
【问题描述】:
我有一个带有搜索功能的网站,但我想查看列入黑名单的关键字的搜索词
问题是列表太大,现在网站加载缓慢(第一个字节 3-5 秒),CPU 负载高。 该列表中有 45.000 行(~1.5 mb)
我尝试拆分成几个较小的文件,但速度仍然很慢
黑名单功能适用于每次搜索 你可以在下面看到它
function CheckForBlockedWords(){
$blackList=array();
$string1 = trim(strtolower(str_replace("_"," ",$_GET['search'])));
$string1 = preg_replace('/-+/', ' ',$string1);
$string1 = preg_replace('/\s+/', ' ',$string1);
$string1 = trim($string1);
foreach (glob("/home/keywords2/*") as $file) {
$blackList = file($file);
foreach($blackList as $word)
{
$string2 = trim(strtolower(str_replace("_"," ",$word)));
$string2 = str_replace("-"," ",$string2);
//$string2 = preg_replace('/-+/', ' ',$string2);
$string2 = preg_replace('/\s+/', ' ',$string2);
$string2 = trim($string2);
if(strpos($string1, $string2) !== false )
{
echo "<div class='blockedSearch'>What you are searching is blocked!</div>";
return true;
}
}
}
return false;
}
有什么方法可以更快地做到这一点?
【问题讨论】:
-
这个问题更适合codereview.stackexchange.com
标签: php arrays preg-replace