title: how to highlight search results
body: <p>hello i have a search function in that i will search a db for keywords. i would like to highlight the keywords and found a second function that i would like to implement into my search function.</p>
  
  <p>so i have this code for the search:</p>
  
  <pre><code>&lt;?php 
  function searchText($keywords){
      global $db;
      $returned_results = array();
      $where2 = "";
  
      $keywords = preg_split('/[\s]+/', $keywords); 
      $total_keywords = count($keywords);
  
      foreach ($keywords as $key=&gt;$keyword){
          $where2 .= "`column` LIKE '%$keyword%'";
  
          if ($key != ($total_keywords - 1)){ 
              $where2 .= " OR ";
          }
      }
  
      $results_text = "SELECT `a`, `b`, LEFT(`c`, 150) as `c` FROM `table` WHERE $where2"; 
      $results_num_text = ($query2 = mysqli_query($db, $results_text)) ? mysqli_num_rows($query2) : 0; 
      if ($results_num_text === 0){
          return false;
      } else {
  
          while ($row = mysqli_fetch_assoc($query2)){
  
              $returned_results[] = array(
                  'ab' =&gt; $row['ab'],
                  'cd' =&gt; $row['cd'], 
              );
          }
  
          return $returned_results;
      }
  }
  ?&gt;
  </code></pre>
  
  <p>and would like to implement a second function into it:</p>
  
  <pre><code>&lt;?php
  function mark_words ($text, $words, $colors = false)
  {
  
      if (!$colors || !is_array($colors) ) {
          $colors = array('#ff9999', '#ffff99', '#ff99ff', '#99ffff','#99ff99');
      }
  
      $c = 0;
  
      foreach ($words as $w) {
  
          $w = preg_quote(trim($w));
  
          if($w=='') {
              continue;
          }
  
          $regexp = "/($w)(?![^&lt;]+&gt;)/i";
  
          $replacement = '&lt;b style="background-color:'.$colors[$c].'"&gt;\\1&lt;/b&gt;';
  
          $text = preg_replace ($regexp,$replacement ,$text);
  
          $c++;
          if ($c &gt;= count($colors)) {
              $c=0;
          }
      }
      return $text;
  }
  
  $example = &lt;&lt;&lt; EOT
  some text is here inside
  EOT;
  
  $search = array('some','is', 'inside');
  
  echo mark_words($example, $search);
  ?&gt; 
  </code></pre>
  
  <p>so i have this code that doesnt work:</p>
  
  <pre><code>&lt;?php 
  function searchText($keywords, $colors = false){
      global $db;
  
       if (!$colors || !is_array($colors) ) {
          $colors = array('#ff9999', '#ffff99', '#ff99ff', '#99ffff','#99ff99');
      }
  
      $c = 0;
  
      $returned_results = array();
      $where2 = "";
  
      $keywords = preg_split('/[\s]+/', $keywords); 
      $total_keywords = count($keywords);
  
      foreach ($keywords as $key=&gt;$keyword){
  
      $regexp = "/($w)(?![^&lt;]+&gt;)/i";
          $replacement = '&lt;b style="background-color:'.$colors[$c].'"&gt;\\1&lt;/b&gt;';
  
          $text = preg_replace($regexp,$replacement ,$keywords);
          $c++;
  
          if ($c &gt;= count($colors)) {
              $c=0;
          }
  
          $where2 .= "`b` LIKE '%$keyword%'";
  
          if ($key != ($total_keywords - 1)){ 
              $where2 .= " OR ";
          }
      }
  
      $results_text = "SELECT `a`, LEFT(`b`, 150) as `b`, `c` FROM `table` WHERE $where2";
      $results_num_text = ($query2 = mysqli_query($db, $results_text)) ? mysqli_num_rows($query2) : 0; 
      if ($results_num_text === 0){
          return false;
      } else {
  
          while ($row = mysqli_fetch_assoc($query2)){
  
              $returned_results[] = array(
                  'ab' =&gt; $row['a'],
                  'cd' =&gt; $row['b'],
              );
          }
  
          return $returned_results;
          $highlight = array($keywords);
          echo mark_words($highlight);
      }
  }
  ?&gt;
  </code></pre>
  
  <p>as i looked for it how to do so i found two possibilities. the first would be a function the second would be directly to highlight it from the select query:</p>
  
  <pre><code>SELECT
          REPLACE(`col`, 'foobar', '&lt;span class="highlight"&gt;foobar&lt;/span&gt;') AS `formated_foobar`
    FROM
          …
    WHERE
          `col` LIKE "%foobar%"
  </code></pre>
  
  <p>so my question is how can i implement the second function into the search function or would it be better to use the second method?</p>
  
  <p>if there is someone who could help me i really would appreciate. thanks a lot.</p>