When was str_ireplace() added?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

When was str_ireplace() added?

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

str_ireplace() wrote:str_ireplace

(PHP 5)
str_ireplace -- Case-insensitive version of str_replace().
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

i would like to use this function ;( but my paid hosting has 4.3.11 as well
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can make your own using regex quite easily...
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

lol, of course Feyd. My bad. ;)
Post Reply