Home > Enterprise >  How to remove   whenever it occurs between two characters?
How to remove   whenever it occurs between two characters?

Time:12-14

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 &nbsp; should be removed as it occurs between the desired characters: {{ $date_today&nbsp; }}</p>

<p>Another element which has a &nbsp; 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('&nbsp;',"", $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 &nbsp;
there is more on the next line &nbsp;
some more";

$newString = str_replace('r&nbsp;d','r d',$string);
echo $newString;

Here is some code to show how it would work.

  • Related