Connecting Tech Pros Worldwide Forums | Help | Site Map

Insensitive Case for str_replace

TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#1: Mar 16 '08
Well after many hours of writting a "sanitizing" function I have discovered I do not have stri_replace installed or something. I don't really want to search for WHERE, WhERE, WheRE... and so on, just to remove WHERE for MySQL input, does anyone have an idea how I could get around it?

I was using ronverdonk's code:
[PHP]str_ireplace( array(" ", "/",",","'","*","and","or","where"),'', 'WHERE This IS a TeSt.' ) );[/PHP]

ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Mar 16 '08

re: Insensitive Case for str_replace


When you do not have PHP5, you can use your own str_ireplace function. Thanks to n00b at battleofthebits in the PHP documentation. [php]if (!function_exists('str_ireplace') {
function str_ireplace($search,$replace,$subject) {
$token = '^[[term^]';
$haystack = strtolower($subject);
$needle = strtolower($search);
while (($pos=strpos($haystack,$needle))!==FALSE) {
$c++;
$subject = substr_replace($subject,$token,$pos,strlen($search ));
$haystack = substr_replace($haystack,$token,$pos,strlen($searc h));
}
while (($pos=strpos($subject,$token))!==FALSE) {
$subject = substr_replace($subject,$replace,$pos,strlen($toke n));
}
return $subject;
}
} [/php]Ronald
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#3: Mar 16 '08

re: Insensitive Case for str_replace


Actually I found this:
[PHP]eregi_replace( array(" ", "/",",","'","*","and","or","where"),'', 'WHERE This IS a TeSt.' )[/PHP]

And it works except for the pattern to replace. In there it is an array, but I believe it needs to be in another form.
[PHP]eregi_replace( "where",'', 'WHERE This IS a TeSt.' )[/PHP]
works, and the php manual says it has to be a POSIX extended regular expression, so, I can do single characters, but not a set of character or a set of words. Any advice or hints?
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#4: Mar 16 '08

re: Insensitive Case for str_replace


No hints. But why don't you want to use the str_ireplace function I posted instead of using a reg exp?

Ronald
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#5: Mar 16 '08

re: Insensitive Case for str_replace


Quote:

Originally Posted by ronverdonk

No hints. But why don't you want to use the str_ireplace function I posted instead of using a reg exp?

Ronald

Because if I do, it converts all the string to lower. I want to retain capitals in names. Acually, I have just thought about a problem. Say someone's name is "Wherever_you_go" that will remove it? I might ahve to think of a better system which allows that. The reg exp doesn't need to convert to lowercase.
Reply