title: PHP : How can I Highlight searched words in results and keep the words original text case?
body: <p>I am displaying search results on site where users can search for specific keyword, words.</p>
  
  <p>On results page I am trying to Highlight the searched words , in the result.
  So user can get idea which words matched where.</p>
  
  <p>e.g.</p>
  
  <pre><code>if user searches for : mango
  
  the resulting item original : This Post contains Mango.
  
  the resulting output I want of highlighted item : This Post contains &lt;strong&gt;Mango&lt;/strong&gt;
  </code></pre>
  
  <p>I am using it like this.</p>
  
  <pre><code>&lt;?php
  
  //highlight all words 
  function highlight_words( $title, $searched_words_array) {
      // loop through searched_words_array
      foreach( $searched_words_array as $searched_word ) {
          $title = highlight_word( $title, $searched_word); // highlight word
      }
  
      return $title; // return highlighted data
  }
  
  //highlight single word with color
  function highlight_word( $title, $searched_word) {
      $replace = '&lt;strong&gt;' . $searched_word . '&lt;/strong&gt;'; // create replacement
      $title = str_ireplace( $searched_word, $replace, $title ); // replace content
      return $title; // return highlighted data
  }
  </code></pre>
  
  <p>I am getting searched words from Sphinx Search Engine , the issue is Sphinx returns entered/macthed words in lowercase.</p>
  
  <p>So by using above code , my </p>
  
  <pre><code>results becomes : This Post contains &lt;strong&gt;mango&lt;/strong&gt;
  </code></pre>
  
  <p><strong>*notice the m from mango got lowercase.</strong></p>
  
  <p>So my question is how can I Highlight word i.e. wrap <code>&lt;strong&gt;</code> &amp; <code>&lt;/strong&gt;</code> around the words matching the Searched words ?
  without loosing its textcase ?</p>
  
  <p>*ppl. its not same questions as <a href="https://stackoverflow.com/questions/10313332/how-to-highlight-search-results">how to highlight search results</a> , I am asking my keywords array is in lowercase and using above method the original word gets replaced by lowercase word.
  so how can I stop that ?
  the other question link will face this too , because the searched keywords are in lowercase. and using <code>str_ireplace</code> it will match it and replace it with lowercase word.</p>
  
  <hr>
  
  <p>update :</p>
  
  <p>i have combined various code snippets to get what i was expecting code to do.,
  for now its working great.</p>
  
  <pre><code>function strong_words( $title, $searched_words_array) {
      //for all words in array
      foreach ($searched_words_array as $word){
  
          $lastPos = 0;
          $positions = array();
          //find all positions of word
          while (($lastPos = stripos($title, $word, $lastPos))!== false) {
             $positions[] = $lastPos;
             $lastPos = $lastPos + strlen($word);
          }
          //reverse sort numeric array 
          rsort($positions);
  
          // highlight all occurances
          foreach ($positions as $pos) {
              $title = strong_word($title , $word, $pos);
          }
      }
  
  //apply strong html code to occurances  
  $title = str_replace('#####','&lt;/strong&gt;',$title);
  $title = str_replace('*****','&lt;strong&gt;',$title);
  return $title; // return highlighted data
  }
  
  
  function strong_word($title , $word, $pos){
  //ugly hack to not use &lt;strong&gt; , &lt;/strong&gt; here directly, as it can get replaced if searched word contains charcters from strong
  $title = substr_replace($title, '#####', $pos+strlen($word) , 0) ;
  $title = substr_replace($title, '*****', $pos , 0) ;
  return $title;
  } 
  
  $title = 'This is Great Mango00lk mango';
  $words = array('man','a' , 'go','is','g', 'strong') ;
  
  echo strong_words($title,$words);
  </code></pre>