Simple PHP search with whole word extract and highlighted search word


/ Published in: PHP
Save to your folder(s)

Simple PHP search with whole word extract and highlighted search word


Copy this code and paste it in your HTML
  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. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.