Sort of a code share/critque here.
This is a small function to perform case-insensitive string replacements.
This is included in PHP 5, but I've seen a couple threads asking this kind of question and being pointed to eregi.
Maybe it's overkill.
Maybe I'm wasting time.
But this is for us eregi-illiterate folk 😃
<?php
function str_ireplace($find,$replace,$string) {
/* Create arrays for $find and $replace
If multiple $replace's are present, but
only one $find, it will replace $find
with only the first value in the
$replace array
*/
if(!is_array($find)) {
$find = array($find);
}
if(!is_array($replace)) {
if(!is_array($find)) {
$replace = array($replace);
}
else {
$r = $replace;
unset($replace);
for($i=0;$i<count($find);$i++) {
$replace[$i] = $r;
}
}
}
foreach($find as $key => $value) {
$between = explode(strtolower($value),strtolower($string));
$pos=0;
foreach($between as $betweenKey => $betweenItem) {
$between[$betweenKey] = substr($string,$pos,strlen($betweenItem));
$pos += strlen($betweenItem) + strlen($value);
}
$string = implode($replace[$key],$between);
}
return $string;
}
/* Example */
$text = "foo bar Foo Bar FOo BAr FOO BAR fOo bAR";
/* Given the following... */
$findThis = array("foo","bar");
$useThis = array("blah","BLEH");
$newText = str_ireplace($findThis, $useThis, $text);
/* $newtext will output
"blah BLEH blah BLEH blah BLEH blah BLEH blah BLEH"
*/
Fairly simple. Hope it helps someone.