Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $checkbox = "false"; // Checkbox selected in form. Use literal string for value of checkbox.
- $keywords = "fix, beacon";// Search terms entered into form. Use combination of known whole words and partial matches for testing.
- $keywords = str_replace(" ","",$keywords);// Close all empty spaces to meet syntax requirements.
- $keywords = explode(",",$keywords); // If there are multiple keywords, convert them into an array, otherwise is_array appears to return single words as 'false'.
- if($checkbox == "true") // If "whole words only" is checked
- {
- if(is_array($keywords))
- {
- $search = "'[[:<:]]".implode("[[:>:]]|[[:<:]]",$keywords)."[[:>:]]'"; // Whole word REGEXP search for multiple keywords.
- }
- else
- {
- $search = "'[[:<:]]".$keywords."[[:>:]]'"; // Whole word REGEXP search for single keywords.
- }
- }
- else
- {
- if(is_array($keywords))
- {
- $search = "'".implode("|",$keywords)."'"; // Any word REGEXP search for multiple keywords.
- }
- else
- {
- $search = "'".$keywords."'"; // Any word REGEXP search for single keywords.
- }
- }
- echo $search; // Error checking
- $sql = ("SELECT column FROM table WHERE column REGEXP $search");
- if(!$result_sql = $mysqli->query($sql))
- {
- echo "It broke"; // Change this to something more appropriate.
- }
- while($result = $result_sql->fetch_assoc())
- {
- $column = $result['column'];
- if($checkbox == "true") // If "whole words only" is checked
- {
- if(is_array($keywords))
- {
- foreach($keywords as $word)
- {
- $pattern = "/b".$word."b/i"; // Adds 'b' before and after the word for whole words only.
- $column = preg_replace($pattern,"<span class="highlight">".$word."</span>", $column); // Applies the highlight CSS to each word found.
- }
- }
- else
- {
- $pattern = "/b".$word."b/i"; // // Adds 'b' before and after the word for whole words only.
- $column = preg_replace($pattern,"<span class="highlight">".$word."</span>", $column); // Applies the highlight CSS to each word found.
- }
- }
- else
- {
- if(is_array($keywords))
- {
- foreach($keywords as $word)
- {
- $pattern = "/".$word."/i";
- $column = preg_replace($pattern,"<span class="highlight">".$word."</span>", $column); // Applies the highlight CSS to each word found.
- }
- }
- else
- {
- $pattern = "/".$word."/i";
- $column = preg_replace($pattern,"<span class="highlight">".$word."</span>", $column); // Applies the highlight CSS to each word found.
- }
- }
- echo "column: ".$column;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement