簡體   English   中英

全文搜索單獨使用PHP

[英]Full text search PHP alone

我有一個InnoDB表,從中檢索值並將其存儲在PHP數組中。

現在,我要根據搜索字符串中的匹配項對數組進行排序。

例如:如果我搜索“ hai,你好嗎”,它將把字符串分成單獨的單詞,如“ hai”,“ how”,“ are”,“ you”,搜索后的結果必須如下:

 [0] hai how are all people there

 [1] how are things going 

 [2] are you coming

 [3] how is sam

...

有什么方法可以單獨根據基本PHP函數中的相關性對數組進行排序?

$searchText = "hai how      are you"; //eg: if there are multiple spaces between words
$searchText = preg_replace("(\s+)", " ", $searchText );
$searchArray =& split( " ", $searchText );

$text = array(0 => 'hai how are all people there',
              1 => 'how are things going ',
              2 => 'are you coming',
              3 => 'how is sam',
              4 => 'testing ggg');

foreach($text as $key=>$elt){
    foreach($searchArray as $searchelt){
        if(strpos($elt,$searchelt)!== FALSE){
            $matches[] =  $key;  //just storing key to avoid memory wastage
            break;
        }                
    }            
}

//print the matched string with help of stored keys
echo '<pre>matched string are as follows: ';
foreach ($matches as $key){

  echo "<br>{$text[$key]}";    
}

也許是這樣的:

$arrayToSort=array(); //define your array here
$query="hai how are you";

function compare($arrayMember1,$arrayMember2){
     $a=similar_text($arrayMember1,$query);
     $b=similar_text($arrayMember2,$query);
     if($a>$b)return 1;
     else return -1;
}

usort($arrayToSort,"compare");

在php手冊中查找有關same_text和usort的用法的說明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM