search and replacing but keeping the letter case in tact

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    #1

    search and replacing but keeping the letter case in tact

    Hello,

    Using PHP 4, how would I do the following? I want to do a case
    insensitive search on a string and replace it with "<span>" tags
    opening and following the string in question. However, when I replace
    the string, I want to preserve whatever the case was before. So, for
    example, given

    $search_str = "abc";
    $main_str = "ABCDE"
    $new_str = highlightSearch ($main_str);

    I would like "$new_str" to contain "<span
    class='highligh t'>ABC</span>DE".

    Does that make sense? Any ideas?

    Thanks, -

  • milahu

    #2
    Re: search and replacing but keeping the letter case in tact

    Use a regular expression :)

    $haystack = preg_replace(
    '/(abc)/i',
    '<span>$1</span>',
    $haystack
    );

    Comment

    • lorento

      #3
      Re: search and replacing but keeping the letter case in tact

      Just a variant method :

      <?php
      $search_str = "abc";
      $main_str = "ABCDE";
      $new_str = eregi_replace ("(abc)", "<span>\\1</span>", $main_str);

      ?>

      Note: eregi_replace slower than preg_replace

      regards,

      Lorento
      --



      milahu wrote:[color=blue]
      > Use a regular expression :)
      >
      > $haystack = preg_replace(
      > '/(abc)/i',
      > '<span>$1</span>',
      > $haystack
      > );[/color]

      Comment

      Working...