PHP strpos和substr_count表现得很奇怪

时间:2014-05-15 19:37:04

标签: php search

我在PHP中创建了一个非常简单的搜索引擎,并且我试图找出搜索词在页面的某些部分出现的次数。 这是相关代码。

$title_count = strpos(strtolower($title),$value);
$tag_count = strpos(strtolower($tags),$value);
$summary_count = substr_count(strtolower($summary),$value);
$body_count = substr_count(strtolower($body),$value);

第一次运行时,平均只需0.000825秒。但是当它以第二个值重复时,它会跳到2.463550秒。然后从那里倍增。

这种行为是正常的吗?或者我是否打算让搜索引擎出错?

$search_term = "";
if(isset($_GET['s'])){
$search_term = trim($_GET['s']);
if($search_term != ""){
  $search_term = htmlspecialchars($search_term);
  $search_term = str_replace("'","''",$search_term);

$term_exploded = explode(" ", $search_term);
$search_array = array();
foreach($term_exploded as $each){
if($each != "" AND !in_array($each, $search_array)){
    $search_array[] = strtolower($each);
        }
    }
}
}


$check = array();
$inhere = array();
$each = array();
$start = microtime(true);
// First | search database for matches 
if($search_term != ""){
foreach($search_array as $key => $value){
$result = mysqli_query($con,"SELECT * FROM articlesearch
        WHERE title LIKE '%$value%' 
        OR summary LIKE '%$value%'  
        OR body LIKE '%$value%' 
        OR tags LIKE '%$value%'");

    while($row = mysqli_fetch_assoc($result)){
        $article_id     = $row['article_id'];
        $user_id            = $row['user_id'];
        $username       = $row['username'];
        $title              = $row['title'];
        $summary            = $row['summary'];
        $body               = $row['body'];
        $tags               = $row['tags'];
        $views              = $row['views'];
        $certified      = $row['certified'];
        $up                     = $row['up'];
        $down               = $row['down'];

        $title_count = strpos(strtolower($title),$value);
        $tag_count = strpos(strtolower($tags),$value);
        $summary_count = substr_count(strtolower($summary),$value);
        $body_count = substr_count(strtolower($body),$value);

        // $check[] = array($title_count, $tag_count, $summary_count, $body_count);

        $sort_number = rand(10000, 11000);

        if(in_array($article_id, $inhere)){
            foreach($each as $key => $value){
                if($value['article_id'] == $article_id){
                    $each[$key]['sort_number'] = $value['sort_number'] + $sort_number;
                }
            }
        }else{
            $inhere[] = $article_id;
            $each[] = array(
                                    'sort_number'       => $sort_number,
                                    'article_id'        => $article_id, 
                                    'user_id'           => $user_id, 
                                    'username'          => $username, 
                                    'title'                 => $title, 
                                    'summary'           => $summary, 
                                    'body'                  => $body, 
                                    'tags'                  => $tags, 
                                    'views'                 => $views, 
                                    'certified'         => $certified, 
                                    'up'                        => $up, 
                                    'down'                  => $down);
        }
    }
  // echo $key." has the value: ". $value . "<br>";
}
}
rsort($each);

有引擎搜索引擎。你可以看到它非常基本。

0 个答案:

没有答案