PDA

View Full Version : Case Insensitive?


Digger3000
04-29-2005, 01:15 AM
Let's say I want to have something like $color="red";
$newcolor="blue";
$newmessage=str_replace($color, $newcolor, $message);

So I want to replace "red" with "blue" but I also want to replace "Red" with "blue". Is there any way I can make that case insensitive?

hemebond
04-29-2005, 03:03 AM
http://us3.php.net/str_ireplace

Bookmark the PHP manual please.

Digger3000
04-29-2005, 03:14 AM
Hey I know where the PHP manual is! Plus, that didn't work. I got a "Call to undefined function str_ireplace()"

Velox Letum
04-29-2005, 03:23 AM
Are you sure you spelled it right?

hemebond
04-29-2005, 03:29 AM
You're using an old version of PHP, in which case you'll have to do it manually using str_replace.

Digger3000
04-29-2005, 03:30 AM
Yeah I'm using php 4.3 or something like that.

Velox Letum
04-29-2005, 03:31 AM
That'll do it. str_ireplace is a PHP5 function.

Kurashu
04-29-2005, 04:41 AM
$red = array('Red', 'red'); //insert other variatiosn as needed

$text = str_replace($red, 'blue', $text);

Digger3000
04-29-2005, 05:21 PM
Yeah I know I could do that but I was looking for a way to make in case insensitive so I wouldn't have to.

Velox Letum
04-29-2005, 05:44 PM
Unfortunately, its either str_ireplace (and by proxy, PHP5) or strtolower() (http://www.php.net/function.strtolower).

oracleguy
04-29-2005, 06:13 PM
Well, you could use a very simple regular expression as well and then use the case-insensitive flag. That would get you a functionality similar to str_ireplace.

mordred
04-29-2005, 07:12 PM
$newmessage = preg_replace('/red/i', 'blue', $message);

marek_mar
04-29-2005, 10:30 PM
(Unfortunetly) Regex is the way untill you get PHP5.