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

3 Answers 3

10

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"
Sign up to request clarification or add additional context in comments.

Comments

4

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).

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.