I am trying to find a way to remove any occurrences of a
character when it occurs between two other characters.
Although i am not sure of the best way of doing this without it affecting the entire text body.
For example, i have the following text, and I want to remove the
if it is between 2 opening curly braces {{
or 2 closing curly braces }}
:
<p>A element where the should be removed as it occurs between the desired characters: {{ $date_today }}</p>
<p>Another element which has a but should not be removed.</p>
CodePudding user response:
There are different options available py pure regex but I'd use a callback for simplilcity.
$str = preg_replace_callback('~\{\{.*?}}~s',
function($m) { return str_ireplace(' ',"", $m[0]);
}, $str);
See this demo at tio.run or check out the pattern explanation at regex101
CodePudding user response:
You can use str_replace to do this.
$string = "this is some random String
there is more on the next line
some more";
$newString = str_replace('r d','r d',$string);
echo $newString;
Here is some code to show how it would work.