如何计算用户提交的数据在存储在数据库问题中的文章中出现的次数


How to count how many times user submitted data is present in an article stored in a database question

我想知道如何计算用户提交的单词在我的MySQL数据库中存储的文章中出现的次数,然后显示从出现次数最高到最低的结果。

这是我的PHP &MySQL代码如下:

$x = 0;
$con = null;
$search = $_REQUEST['search'];
$search_explode = mysqli_real_escape_string($dbc, $search);
$search_explode = explode(' ', $search_explode);
foreach($search_explode as $search_each) {
    $x++;
    if($x == 1){
        $con .= " article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
    } else {
        $con .= " OR article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
    }
}
$con = "SELECT users.*, users_articles.* FROM users_articles
              INNER JOIN users ON users_articles.user_id = users.user_id
              WHERE ($con) 
              AND users.active IS NULL
              AND users.deletion = 0";
$run =  mysqli_query($dbc, $con);
$search_term = mysqli_num_rows($run);

一旦您将文章作为字符串存储在某个变量中,您就可以使用substr_count来查找特定字符串的出现次数。

如果您想要关于文章中使用的单词的一般信息,您可以使用str_word_count获取字符串中所有单词的列表,然后使用该列表。

您想要查找一个单词在字符串中出现的所有情况:

<?php 
function findall($needle, $haystack) 
{ 
    //Setting up 
    $buffer=''; //We will use a 'frameshift' buffer for this search 
    $pos=0; //Pointer 
    $end = strlen($haystack); //The end of the string 
    $getchar=''; //The next character in the string 
    $needlelen=strlen($needle); //The length of the needle to find (speeds up searching) 
    $found = array(); //The array we will store results in 
    while($pos<$end)//Scan file 
    { 
        $getchar = substr($haystack,$pos,1); //Grab next character from pointer 
        if($getchar!="'n" || buffer<$needlelen) //If we fetched a line break, or the buffer is still smaller than the needle, ignore and grab next character 
        { 
            $buffer = $buffer . $getchar; //Build frameshift buffer 
            if(strlen($buffer)>$needlelen) //If the buffer is longer than the needle 
            { 
                $buffer = substr($buffer,-$needlelen);//Truncunate backwards to needle length (backwards so that the frame 'moves') 
            } 
            if($buffer==$needle) //If the buffer matches the needle 
            { 
                $found[]=$pos-$needlelen+1; //Add the location of the needle to the array. Adding one fixes the offset. 
            } 
        } 
        $pos++; //Increment the pointer 
    } 
    if(array_key_exists(0,$found)) //Check for an empty array 
    { 
        return $found; //Return the array of located positions 
    } 
    else 
    { 
        return false; //Or if no instances were found return false 
    } 
} 
?> 

from http://php.net/manual/en/function.strstr.php

和另一个:

 <?php
function find_occurences($string, $find) {
    if (strpos(strtolower($string), strtolower($find)) !== FALSE) {
        $pos = -1;
        for ($i=0; $i<substr_count(strtolower($string), strtolower($find)); $i++) {
            $pos = strpos(strtolower($string), strtolower($find), $pos+1);
            $positionarray[] = $pos;
        }
        return $positionarray;
    }
    else {
        return FALSE;
    }
}
从http://www.phpfreaks.com/forums/index.php?topic=195567.0

这是非常容易使用全文搜索。例如:

SELECT *,
MATCH(title, body) AGAINST ('PHP') AS score
FROM articles
WHERE MATCH(title, body) AGAINST('PHP') 

根据MySQL手册,全文是一个"自然语言搜索";它使用指定的列对似乎表示该行的单词进行索引。例如,如果所有行都包含"MySQL",那么"MySQL"将不匹配太多。它不是唯一的,它会返回太多结果。但是,如果"MySQL"只出现在5%的行中,它将返回这些行,因为它不太经常出现,而这是一个非常常见的关键字。(如果你没有"MySQL"在你的行,它将返回什么;咄。)

MySQL还做了一些非常有用的事情。它创造了一个分数。这个分数通常类似于。9823475或。124874,但总是大于零。它的范围可以大于1,我有时看到它是4。(不要试图将其乘以100并将其描绘为%值;人们会想知道为什么他们的关键词匹配一篇文章的431%!)

MySQL也会按评分降序排列。

另一个有用的提示:如果您使用MATCH() AGAINST()在查询中将this的文档样式更改为"Inline Code"两次,就像我们将要做的那样,没有额外的速度损失。您可能会认为,由于两次执行相同的搜索,因此查询将花费两倍的时间,但实际上MySQL在运行第二次搜索时记住了第一次搜索的结果。

了解更多信息请参见:http://devzone.zend.com/article/1304