Reputation: 183
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
Upvotes: 3
Views: 119
Reputation: 526533
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).
Upvotes: 4
Reputation: 175325
You could use a regular expression, but PHP also conveniently has a case-insensitive version of str_replace
, str_ireplace
Upvotes: 3
Reputation: 454940
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"
Upvotes: 10