title: Highlighting all matched keyword in strings in case insensitive manner and preserve case in the returned tex
body: <p>I am trying to highlight/mark all occurrences of a string in another string. 
  i searched and find this:</p>
  
  <p><a href="http://www.php.net/manual/en/function.str-ireplace.php#87417" rel="nofollow">http://www.php.net/manual/en/function.str-ireplace.php#87417</a></p>
  
  <p>but minute after that i have found bug/issue in it (may be i am wrong too)</p>
  
  <p>however here is my code:</p>
  
  <pre><code>&lt;?php
  
  $strMain = 'This is cOloR One, this is CoLOR two and third color this one!';
  $strFind = 'color';
  
  echo highlightStr($strMain, $strFind, '#FF0000');
  
  function highlightStr($haystack, $needle, $highlightColorValue)
  {
       // return $haystack if there is no highlight color or strings given, nothing to do.
      if (strlen($highlightColorValue) &lt; 1 || strlen($haystack) &lt; 1 || strlen($needle) &lt; 1) {
          return $haystack;
      }
      preg_match_all("/$needle+/i", $haystack, $matches);
      if (is_array($matches[0]) &amp;&amp; count($matches[0]) &gt;= 1) {
          foreach ($matches[0] as $match) {
              $haystack = str_replace($match, '&lt;span style="color:'.$highlightColorValue.';"&gt;'.$match.'&lt;/span&gt;', $haystack);
          }
      }
      return $haystack;
  }   
  ?&gt;
  </code></pre>
  
  <p>if you run the code, then you will see the issue. Result will be even worse if you search for a single letter like 'o'.. </p>
  
  <p>so, is there any better/perfect solution to that?</p>