Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. <?php
  2. $checkbox = "false"; // Checkbox selected in form. Use literal string for value of checkbox.
  3.  
  4. $keywords = "fix, beacon";// Search terms entered into form. Use combination of known whole words and partial matches for testing.
  5. $keywords = str_replace(" ","",$keywords);// Close all empty spaces to meet syntax requirements.
  6. $keywords = explode(",",$keywords); // If there are multiple keywords, convert them into an array, otherwise is_array appears to return single words as 'false'.
  7.  
  8. if($checkbox == "true") // If "whole words only" is checked
  9. {
  10. if(is_array($keywords))
  11. {
  12. $search = "'[[:<:]]".implode("[[:>:]]|[[:<:]]",$keywords)."[[:>:]]'"; // Whole word REGEXP search for multiple keywords.
  13. }
  14. else
  15. {
  16. $search = "'[[:<:]]".$keywords."[[:>:]]'"; // Whole word REGEXP search for single keywords.
  17. }
  18. }
  19. else
  20. {
  21. if(is_array($keywords))
  22. {
  23. $search = "'".implode("|",$keywords)."'"; // Any word REGEXP search for multiple keywords.
  24. }
  25. else
  26. {
  27. $search = "'".$keywords."'"; // Any word REGEXP search for single keywords.
  28. }
  29. }
  30. echo $search; // Error checking
  31.  
  32. $sql = ("SELECT column FROM table WHERE column REGEXP $search");
  33. if(!$result_sql = $mysqli->query($sql))
  34. {
  35. echo "It broke"; // Change this to something more appropriate.
  36. }
  37.  
  38. while($result = $result_sql->fetch_assoc())
  39. {
  40. $column = $result['column'];
  41. if($checkbox == "true") // If "whole words only" is checked
  42. {
  43. if(is_array($keywords))
  44. {
  45. foreach($keywords as $word)
  46. {
  47. $pattern = "/b".$word."b/i"; // Adds 'b' before and after the word for whole words only.
  48. $column = preg_replace($pattern,"<span class="highlight">".$word."</span>", $column); // Applies the highlight CSS to each word found.
  49. }
  50. }
  51. else
  52. {
  53. $pattern = "/b".$word."b/i"; // // Adds 'b' before and after the word for whole words only.
  54. $column = preg_replace($pattern,"<span class="highlight">".$word."</span>", $column); // Applies the highlight CSS to each word found.
  55. }
  56. }
  57. else
  58. {
  59. if(is_array($keywords))
  60. {
  61. foreach($keywords as $word)
  62. {
  63. $pattern = "/".$word."/i";
  64. $column = preg_replace($pattern,"<span class="highlight">".$word."</span>", $column); // Applies the highlight CSS to each word found.
  65. }
  66. }
  67. else
  68. {
  69. $pattern = "/".$word."/i";
  70. $column = preg_replace($pattern,"<span class="highlight">".$word."</span>", $column); // Applies the highlight CSS to each word found.
  71. }
  72. }
  73. echo "column: ".$column;
  74. }
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement