I have multiple strings with same curly braces I want to replace them as dynamic if I get the count as 1 then need to replace the first occurrence, If count as 2 then replaces t
The expression we might wish to design here can be like one of these:
({{)(.+?)(}})
which is only using capturing groups ()
.
(?:{{)(.+?)(?:}})
and here we can use non-capturing groups (?:)
, if we do not wish to keep the {{
and }}
.
Then, we could simply do the preg_replace
that we wanted to do.
$re = '/(?:{{)(.+?)(?:}})/m';
$str = '{{ONE}} {{TWO}} {{THREE}} {{FOUR}} {{FIVE}} {{SIX}}';
$subst = '$1';
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;