2012-08-30 70 views
0

我有一個搜索引擎用於我的網站。我已經安裝了Google的功能「你是不是指xxx?」。它返回更正的字符串。那麼,有沒有辦法讓我可以比較那些不正確和正確的字符串,並像谷歌那樣大膽地修正糾正的單詞?如何獲取php中的兩個單詞之間的差異

這裏是我的代碼:

<?php 
function suggestion($word){ 
    $first_word=substr($word,0,1); 
    $first_word=strtoupper($first_word); 
    $query="SELECT * FROM words WHERE UPPER(LEFT(name,1))='$first_word'"; 
    $db_words=mysql_query($query); 
    $flag=true; 
    while($row_words=mysql_fetch_array($db_words)){ 
     similar_text($row_words['name'],$word,$percent); 
     if(($percent>50)&&($percent!=100)){ 
      echo '<strong>Did you mean:</strong><br>'; 
      echo $row_words['name'],'<br>'; 
      $flag=false; 
     } 
    } 
} 
?> 
+0

向我們展示你的代碼 – jeremy

+0

重複:http://stackoverflow.com/questions/321294/highlight-the-difference-between-two-字符串在PHP –

回答

3

根據下面你的評論正在尋找這樣的事情:

function corrected($bad, $good, $wholephrase) { 
    return preg_replace('#\b'. preg_quote($bad) . '\b#g', '<em>' . htmlspecialchars($good) . '</em>', $wholephrase); 
} 

對於那些你們誰是感興趣的是如何檢測拼寫錯誤和替代品的建議是完成看看這裏:Levenshtein Distance

+0

不,我已經有一個使用similar_text的算法現在,我想要在字符串中進行更正高亮 –

+1

然後它是一個好主意,實際上***粘貼** *你的代碼,所以我們可以看到你在做什麼,而不是猜測... – jeremy

+0

嘿,結帳我使用的代碼 –

0

沒有人幫助我! ;-(

這裏是我創建的代碼:

function suggestion($word){ 
    $first_word=substr($word,0,1); 
    $first_word=strtoupper($first_word); 
    $query="SELECT * FROM words WHERE UPPER(LEFT(name,1))='$first_word'"; 
    $db_words=mysql_query($query); 
    $wd=explode(" ",$word); 
    while($row_words=mysql_fetch_array($db_words)){ 
     similar_text($row_words['name'],$word,$percent); 
     if(($percent>50)&&($percent!=100)){ 
      $rw=explode(" ",$row_words['name']); 
      $d=array_udiff($rw,$wd,'strcasecmp'); 
      for($i=0;$i<count($rw);$i++){ 
       if(in_array($rw[$i],$d)){ 
        $rw[$i]="<em>".$rw[$i]."</em>"; 
       } 
      } 
      echo '<strong>Did you mean:</strong><br>'; 
      foreach($rw as $r){ 
       echo $r," "; 
      } 
      echo '<br>'; 
     } 
    } 
} 
相關問題