Jake Jake - 1 month ago 9
PHP Question

Remove dots inside a tags in php

How to remove dots inside a tags in php by preg_replace ?

i tried with this code:

$str = 'out . out<a href="http://example.com">inside. in.si. d .e</a> outside';

$str = preg_replace("/(<a.*>.*)[.](.*<\/a>)/","$1$2", $str);

correct output is:

out . out<a href="http://example.com">inside insi d e</a> outside

But this only clears one dot.


Answer Source

How about repeating the application of your replacement, if it correctly replaces one dot until no change is made?

$oldlen = 0;
while ($oldlen != strlen($str))
  $oldlen = strlen($str);
  $str = preg_replace("/(<a.*>.*)[.](.*<\/a>)/","$1$2", $str);