Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何计算用户提交的数据在存储在数据库中的文章中出现的次数_Php_Mysql - Fatal编程技术网

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

Php 如何计算用户提交的数据在存储在数据库中的文章中出现的次数,php,mysql,Php,Mysql,我想知道如何计算一个用户提交的单词在存储在MySQL数据库中的文章中出现的次数,然后从出现次数的最高点到最低点显示结果 下面是我的PHP和MySQL代码的一部分 $x = 0; $con = null; $search = $_REQUEST['search']; $search_explode = mysqli_real_escape_string($dbc, $search); $search_explode = explode(' ', $search_explode); foreac

我想知道如何计算一个用户提交的单词在存储在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);

要查找字符串中出现的所有单词:

<?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 
    } 
} 
?> 

还有一个:

 <?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;
    }

}

将文章作为字符串存储在某个变量中后,可以使用查找特定字符串的出现次数


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

使用全文搜索非常简单。例如:

SELECT *,
MATCH(title, body) AGAINST ('PHP') AS score
FROM articles
WHERE MATCH(title, body) AGAINST('PHP') 
根据MySQL手册,全文是“自然语言搜索”;它使用指定的列对显示为表示行的单词进行索引。例如,如果您的所有行都包含“MySQL”,那么“MySQL”将不会匹配太多。它不是非常独特,而且会返回太多的结果。然而,如果“MySQL”仅出现在5%的行中,它将返回这些行,因为它不太常见,不太可能被称为关键字。(如果您的所有行中都没有“MySQL”,那么它将不返回任何内容;duh。)

MySQL也做了一些非常有用的事情。它创造了一个分数。该分数通常为.9823475或.124874,但始终大于零。它的范围可以超过1,我有时看到它在4。(不要试图将其乘以100,并将其描述为%值;人们会想知道为什么他们的关键字与文章431%匹配!)

MySQL还将按分数降序排列一行

另一个有用的小贴士是:如果在查询中使用MATCH()对()将其文档样式更改为两次“内联代码”,那么就不会有额外的速度损失。您可能会想到,由于执行相同的搜索两次,查询将花费两倍的时间,但事实上MySQL在运行第二次搜索时会记住第一次搜索的结果


要了解更多信息,请参阅:

我希望可以使用全文搜索,但我使用的是PHP5.2:(这是MySQL中的功能,应该与PHP版本无关。