I'm looking for a variation of str_ireplace .. where only one definition of search phase (e.g. ALPHA) is needed to replace any variation of upper case / lower case (e.g. ALPHA or alpha or Alpha or AlPhA or aLpHa). I don't want to define all possible variations of spelling in $search array. e.g. 'ALPHA, 'alpha', 'Alpha'

Would preg_replace (case insensitive regex) be better for this?

<?php
$haystack = "alpha beta gamma delta lambda ALPHA BETA GAMMA DELTA LAMBDA";
$br = '<br />';
$search = array('ALPHA','BETA');
$count = count ($search);
// wrap $search in font tags
for ($i=0; $i < $count; $i++)
{
$replace[$i] = '<b><font color="blue">' . $search[$i] . '</font></b>';
}
// case insensitive version of str_replace
$haystack = str_ireplace($search, $replace, $haystack, $count);
echo $haystack;
?>

    Would preg_replace (case insensitive regex) be better for this?

    Yes.

      .. well, thanks, I did try this ..

      $tag1 = '<b><font color="blue">';
      $tag2 = '</font></b>';
      $haystack = "alpha beta gamma delta lambda ALPHA BETA GAMMA DELTA LAMBDA";
      $haystack = preg_replace("/ALPHA/i", $tag1 . "alpha" . $tag2, $haystack);
      $haystack = preg_replace("/BETA/i", $tag1 . "beta" . $tag2, $haystack);
      echo $haystack;
      

      .. but I want the original case in $haystack target to be preserved.

        A regexp would help you find the words, but it might take some more coding to get the replacement to duplicate the same capitalization as the search value. Perhaps something with [man]preg_replace_callback/man could be done to locate the strings to be replaced, then use the callback function to determine what sort of capitalization was used, maybe using functions like [man]strtoupper/man and [man]ucfirst/man.

          Try something like this:

          $tag1 = '<b><font color="blue">';
          $tag2 = '</font></b>';
          $haystack = "alpha beta gamma delta lambda ALPHA BETA GAMMA DELTA LAMBDA";
          $haystack = preg_replace('/(ALPHA)/i', $tag1 . '\1' . $tag2, $haystack);
          $haystack = preg_replace('/(BETA)/i', $tag1 . '\1' . $tag2, $haystack);
          echo $haystack;

            Thanks ... this latter code does the trick and preserves the original syntax .. now I have to understand from the manual how it works.

              now I have to understand from the manual how it works.

              The PHP manual entry on [man]preg_replace/man would be the first place to look. The next would be the entry on PCRE syntax concerning capturing subpatterns.

              Incidentally, I note that the PHP manual states that the \n syntax is deprecated in favour of the $n syntax (I thought it was the other way round). In that case, you should write:

              $tag1 = '<b><font color="blue">';
              $tag2 = '</font></b>';
              $haystack = "alpha beta gamma delta lambda ALPHA BETA GAMMA DELTA LAMBDA";
              $haystack = preg_replace('/(ALPHA)/i', $tag1 . '$1' . $tag2, $haystack);
              $haystack = preg_replace('/(BETA)/i', $tag1 . '$1' . $tag2, $haystack);
              echo $haystack;

                One further question .. to improve search performance how can I adapt the above code to use a long array of search words? With the current code I must search through $haystack for each matching.

                $search = array('ALPHA', 'BETA', 'GAMMA', 'DELTA', 'LAMBDA');
                

                  I am not sure if it will affect performance, but you could write:

                  <?php
                  $tag1 = '<b><font color="blue">';
                  $tag2 = '</font></b>';
                  $haystack = "alpha beta gamma delta lambda ALPHA BETA GAMMA DELTA LAMBDA";
                  $search = array('/(ALPHA)/i', '/(BETA)/i', '/(GAMMA)/i', '/(DELTA)/i', '/(LAMBDA)/i'); 
                  $haystack = preg_replace($search, $tag1 . '$1' . $tag2, $haystack);
                  echo $haystack;
                  ?>

                    O.K. this variant now works with a search array ..

                    <?php
                    $tag1 = '<b><font color="blue">';
                    $tag2 = '</font></b>';
                    
                    $haystack = "alpha beta gamma delta lambda ALPHA BETA GAMMA DELTA LAMBDA";
                    
                    $search = array('ALPHA', 'BETA'); 
                    
                    $count = count($search);
                    
                    for ($i=0; $i < $count; $i++)
                    {
                    $haystack = preg_replace('/(' . $search[$i] . ')/i', $tag1 . '$1' . $tag2, $haystack);	
                    }
                    echo $haystack;
                    ?>
                    

                    Thanks for your help.

                    p.s. .. but I note that your version does not loop through count so it should be faster.

                      laserlight's version would be more efficient since there's no need to loop through an array; as noted in the manual for [man]preg_replace/man, the pattern can be a string or an array.

                      Don't forget to mark this thread resolved.

                        Write a Reply...