case insensitive string search and replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

case insensitive string search and replace

Post by pthomas »

My next problem. I need to be able to take one string (user input) and see if another string exists within the uer input. If that string is found, I want to remove it. But I also don;t want the search to be case-sensitive. The user input needs to be able to have upper and lower case chars in it and retain its case. So switching it all to lower case isn't an option.

For example, I want to take the user string:
You are so dumb that I .......
and find and remove any of the following with the case not being a factor:
you are so
you're so
your so
So after I run the user's sentence through it will strip out the "You are so" part and just give me
dumb that I ......
I did get this partially working, but it only matches items of the same exact case:

Code: Select all

while ($row = mysql_fetch_array($result))
                        $needlesї] = $rowї'phrase'];

                foreach ($needles as $needle)
                {
//                      echo "<BR>Searching "$data" for "$needle".";
                        $data = str_replace( ($needle." "), "", $data);
                &#125;
So that will NOT remove the "You are so", it will only remove the case matching "you are so". Anyone know how to do this? I've been looking through the manual of string functions on php.net and no luck.

Thanks,
Paul
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Read up on regular expressions.... you wouldn't have to do anything complicated with them for this...
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post by pthomas »

Thanks, that got me in the right direction. I ended up using eregi_replace worked real good!

thanks,
Paul
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use preg* over ereg* ... it'll shave some time off the page request requirement.
Post Reply