tags:

views:

50

answers:

3

This is my first question on this wonderful website.

Lets say I have a string $a="some text..%PROD% more text" There will be just one %..% in the string. I need to replace PROD between the % with another variable content. So I used to do:

$a = str_replace('%PROD%',$var,$a);

but now the PROD between % started coming in different cases. So I could expect prod or Prod. So I made the entire string uppercase before doing replacement. But the side effect is that other letters in the original string also became uppercase. Someone suggested me to use regular expression. But how ?

Thanks,

Rohan

+7  A: 

You can make use of str_ireplace function. Its similar to str_replace but is case insensitive during matching.

$x = 'xxx';
$str = 'abc %Prod% def';
$str = str_ireplace('%PROD%',$x,$str); // $str is now "abc xxx def"
codaddict
+1  A: 

You could use a regular expression, but PHP also conveniently has a case-insensitive version of str_replace, str_ireplace

Michael Mrozek
+1  A: 

Just use str_ireplace(). It's a case-insensitive version of str_replace(), and much more efficient for a simple replacement than regular expressions (also much more straightforward).

Amber