Page 1 of 1

When was str_ireplace() added?

Posted: Fri Sep 23, 2005 7:37 pm
by Skara
I have 4.3.11 and I'm getting 'call to undefined function' when I try to use it.

Edit: hah. vim at least thinks it's valid. :P

Posted: Fri Sep 23, 2005 7:43 pm
by feyd
str_ireplace() wrote:str_ireplace

(PHP 5)
str_ireplace -- Case-insensitive version of str_replace().

Posted: Sat Sep 24, 2005 5:00 am
by s.dot
i would like to use this function ;( but my paid hosting has 4.3.11 as well

Posted: Sat Sep 24, 2005 5:35 am
by feyd
you can make your own using regex quite easily...

Posted: Sat Sep 24, 2005 7:50 am
by Nathaniel
I like this method more than regex'n it...

I saw this code somewhere else, but I don't remember where. I'll just type it up again.

Code: Select all

function str_ireplace($search, $replace, $subject) //ignore count parameter, we obviously don't have PHP 5
{
return str_replace ( strlower($search), strlower($replace), strlower($search) );
}
If you're jumping back and forth between a PHP 5 and a PHP 4 server, throw some sort of !function_exists block around it so that it is only defined if the function doesn't already exist.

- Nathaniel

Posted: Sat Sep 24, 2005 8:38 am
by feyd
too bad that forces the output to be lower case... ;)

Code: Select all

if(!function_exists('str_ireplace')) {
function str_ireplace($search, $replace, $subject, &$count = false) {
  if(!is_array($search)) {
    $search = array($search);
  }
  if(!is_array($replace)) {
    $replace = array($replace);
  }
  $search = array_map(CreateFunction('$a','return \'#\'.preg_quote($a,\'#\').\'#i\';'),$search);
  return preg_replace($search,$replace,$subject,-1,$count);
}
}
untested..

of course, you could use the PHP Compatibility library from Pear: http://pear.php.net/package/PHP_Compat

Posted: Sat Sep 24, 2005 10:45 am
by Skara
oh. Well, I easily solved my problem just by using preg_replace. Didn't see where it said php5, though. I run my own server, but I'm too lazy to update. :P

Posted: Sat Sep 24, 2005 10:48 am
by Nathaniel
lol, of course Feyd. My bad. ;)