Replacing strings in PHP

This is my first question on this wonderful site.

Suppose I have a line. $a="some text..%PROD% more text"There will be only one line %..%. I need to replace PRODbetween the %other contents of a variable. So I did:

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

but now PRODbetween %began to arrive in different cases. So I could expect prod or prod. So I made the entire line in uppercase before doing the replacement. But a side effect is that the other letters in the original string also became uppercase. Someone suggested I use regex. But how?

Thank,

Rohan

+3
source share
3 answers

str_ireplace. str_replace, .

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

str_ireplace(). str_replace() , ( ).

+4

You can use regex, but PHP also has a case-insensitive version str_replace,str_ireplace

+3
source

Source: https://habr.com/ru/post/1743117/


All Articles