Insensitive Case for str_replace  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | |
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]
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | 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
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | | 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?
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | 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
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | | 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.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|