PHP - Replacing ereg with preg

I am trying to remove deprecated code from a site. Can anyone tell me a preliminary equivalent

ereg_replace("<b>","<strong>",$content);

      

Thank.

+2


source to share


2 answers


There seems to be no need for regular expressions.

simple str_replace will do:

$cleaned = str_replace  ('<b>', '<strong>', $unCleaned);

      



If you need more complex replacements like attribute validation, you can do:

$cleaned = preg_replace('/<b(\s[^>]*)?>/', '<strong\\1>', $unCleaned);

      

But this is by no means ideal; something like <div title="foo->bar"></div>

will break the regex.

+9


source


PCRE is the equivalent of ERE regex:

preg_match("/<b>/", "<strong>", $content)

      



But as Jacco already pointed out, you don't need a regex at all as you want to replace a constant value.

+3


source







All Articles