简单的PHP搜索,带有整词提取和突出显示的搜索词

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的PHP搜索,带有整词提取和突出显示的搜索词相关的知识,希望对你有一定的参考价值。

Simple php search with whole word extract and highlighted search word
  1. /**
  2.  * Case in-sensitive array_search() with partial matches
  3.  *
  4.  * @param string $needle The string to search for.
  5.  * @param array $haystack The array to search in.
  6.  *
  7.  * @author Bran van der Meer <[email protected]>
  8.  * @since 29-01-2010
  9.  */
  10. function array_find($needle, array $haystack)
  11. {
  12. foreach ($haystack as $key => $value) {
  13. if (false !== stripos($needle, $value)) {
  14. return $key;
  15. //break;
  16. }
  17. }
  18. return false;
  19. }
  20.  
  21. /* Rest of code Flat Earth */
  22.  
  23. $search = strtoupper($search);
  24. $search = strip_tags($search);
  25. $search = trim($search);
  26.  
  27. $sql_srch = "SELECT * FROM `pg_pages` WHERE upper(`story`) LIKE'%$search%'";
  28. $result_srch = doSQL($sql_srch);
  29. $c_srch=mysql_num_rows($result_srch);
  30. for($i_srch=0; $i_srch<$c_srch; $i_srch++)
  31. {
  32. $r_srch = mysql_fetch_array( $result_srch );
  33. $srchUrl = $r_srch['tier1'].$r_srch['tier2'].$r_srch['tier3'] ;
  34. echo '<a href="'.$srchUrl.'">'.$r_srch['title'].'</a>';
  35.  
  36. $stry = $r_srch['story'];
  37. $stry = ereg_replace ( "<h1>(.*)</h1>", "", $stry );
  38. $stry = strip_tags($stry);
  39.  
  40. $stryArray = explode(" ", $stry);
  41. $foundPosition = FALSE;
  42. $foundPosition = array_search( strtolower($search), array_map('strtolower',$stryArray) );
  43. // * Rare instance where search for 'founded' finds 'co-founded' therefore isn't matched
  44. // * So search for fragment of search phrase
  45. // * WARNING: means 'found' is found first if it exists, but at least gives result
  46. if($foundPosition===FALSE)
  47. {
  48. $foundPosition = array_find( $search, $stryArray );
  49. $stryWord = '<span class="notfound">' . implode(' ', array_slice($stryArray, $foundPosition, 1) ). '</span>';
  50. }
  51. else
  52. {
  53. $stryWord = '<span class="found">' . implode(' ', array_slice($stryArray, $foundPosition, 1) ). '</span>';
  54. }
  55.  
  56. $extractWords = 11;
  57.  
  58. $startPosition = ($foundPosition-$extractWords > 0)?$extractWords:$foundPosition;
  59. $startEllipsis = ($startPosition<$extractWords)?'':'...';
  60. $endPosition = ($foundPosition+1+$extractWords < count($stryArray) )?$extractWords:count($stryArray)-$foundPosition;
  61. $endEllipsis = (count($stryArray)-$foundPosition<=$extractWords)?'':'...';
  62.  
  63. $stryStart = implode(' ', array_slice($stryArray, $foundPosition-$startPosition, $startPosition) );
  64. $stryEnd = implode(' ', array_slice($stryArray, $foundPosition+1, $endPosition) );
  65. //$stryWord = '<span class="found">' . implode(' ', array_slice($stryArray, $foundPosition, 1) ). '</span>';
  66. $fndstry = $startEllipsis. $stryStart .' '. $stryWord .' '. $stryEnd .$endEllipsis;
  67.  
  68. echo "<p>".$fndstry."</p>";
  69. }
  70.  
  71. if ($c_srch == 0) {
  72. echo "<p>Sorry, but we can&rsquo;t find anything that matches your search criteria.</p>";
  73. }

以上是关于简单的PHP搜索,带有整词提取和突出显示的搜索词的主要内容,如果未能解决你的问题,请参考以下文章

csharp 带有突出显示的完整ajax搜索示例

PHP 搜索词突出显示

制作一个简单的搜索功能,使光标跳转到(或突出显示)要搜索的单词

搜索词突出显示

在 HTML5 视频中突出显示播放器搜索栏

PHP/MySQL:突出显示“SOUNDS LIKE”查询结果