SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can I make str_replace not case senstive?

    I'm doing a simple search function, and for the results, I want to make all the bits that has the string in it bold. eg:

    PHP Code:
    $query cash;
    $result Cashmere;

    $result str_replace ("$query""<b>$query</b>""$result");

    echo 
    $result
    That would print "Cashmere" instead of "<b>Cash</b>mere" since it's different case, is there a way to get around that, or do I have to use the slower eregi_replace?

  2. #2
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If case sensitivity is needed you would have to use eregi_replace unless you built your own custom function.
    Wayne Luke
    ------------


  3. #3
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could run a strtolower function on both parameters before doing the replacement.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by W. Luke
    If case sensitivity is needed you would have to use eregi_replace unless you built your own custom function.
    you mean case INsensitive.

    Originally posted by aspen
    You could run a strtolower function on both parameters before doing the replacement.
    wouldn't that make the result lowercase? if so he probably doesn't want that.

    Robo, i'd just use preg_replace() which is about 2x faster than eregi_replace().

    PHP Code:
    $query 'cash';
    $result 'Cashmere';

    $query preg_quote($query'/'); // only needed if your 'query' has special reg exp chars in it

    $result preg_replace("/($query)/i"'<b>$1</b>'$result);

    echo 
    $result
    in preg_replace() there you need PHP 4.0.4 or greater to use the $1 part. if your PHP is older than that, use \1. $1 is the preferred way though.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •