Discussion:
best way todo a case insensitive str_replace
pmpa
2005-02-21 15:54:33 UTC
Permalink
Hi all.

What is the best way to do a string insensitive replace?
Currently I am doing:

$replace = "g r";
$arr = explode(" ",$replace);
$text = "PHP is GreaT!";
for($i=0;i<count($arr);$i++){
$text =
str_replace(strtolower($arr[$i]),"<b>".strtolower($arr[$i])."</b>",$text);
$text =
str_replace(strtoupper($arr[$i]),"<b>".strtoupper($arr[$i])."</b>",$text);
}

Works except for "Ph","PhP","gr" etc...
I am looking for suggestions before using str_split(); because my $replace
string can be a bit large :)


Thanks in advance.

Pedro.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Guillermo Rauch
2005-02-21 15:59:01 UTC
Permalink
http://ar2.php.net/str_ireplace
Post by pmpa
Hi all.
What is the best way to do a string insensitive replace?
$replace = "g r";
$arr = explode(" ",$replace);
$text = "PHP is GreaT!";
for($i=0;i<count($arr);$i++){
$text =
str_replace(strtolower($arr[$i]),"<b>".strtolower($arr[$i])."</b>",$text);
$text =
str_replace(strtoupper($arr[$i]),"<b>".strtoupper($arr[$i])."</b>",$text);
}
Works except for "Ph","PhP","gr" etc...
I am looking for suggestions before using str_split(); because my $replace
string can be a bit large :)
Thanks in advance.
Pedro.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jay Blanchard
2005-02-21 15:59:29 UTC
Permalink
[snip]
What is the best way to do a string insensitive replace?
[/snip]

http://us2.php.net/manual/en/function.eregi-replace.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ville Mattila
2005-02-21 16:02:03 UTC
Permalink
Post by pmpa
What is the best way to do a string insensitive replace?
I would encourage you to use eregi_replace function that uses regular
expressions.

$text = eregi_replace("([gr])", "<b>\\1</b>", $text);

Ville
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
John Nichel
2005-02-21 16:04:59 UTC
Permalink
Post by pmpa
Hi all.
What is the best way to do a string insensitive replace?
$replace = "g r";
$arr = explode(" ",$replace);
$text = "PHP is GreaT!";
for($i=0;i<count($arr);$i++){
$text =
str_replace(strtolower($arr[$i]),"<b>".strtolower($arr[$i])."</b>",$text);
$text =
str_replace(strtoupper($arr[$i]),"<b>".strtoupper($arr[$i])."</b>",$text);
}
Works except for "Ph","PhP","gr" etc...
I am looking for suggestions before using str_split(); because my $replace
string can be a bit large :)
You could use a regular expression...

preg_replace ( "/needle/i", "replace", $haystack );
------------------------^

The 'i' makes it case insensitive.
--
John C. Nichel
ÜberGeek
KegWorks.com
716.856.9675
***@kegworks.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...