views:

488

answers:

2

Hi, I'm trying to remove some deprecated code from a site. Can anyone tell me the preg equivalent of

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

Thanks.

+8  A: 

There seems to be no need for regular expressions at all.

a simple str_replace would do:

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

If you need more complicated replacements, for example checking the attributes, you could do:

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

But this is by no means perfect; something like <div title="foo->bar"></div> would break the regular expression.

Jacco
Your current expression will replace any tag starting with `b` (like `base`, `body` or `br`), not just `b` tags. You should use `/<b(\s[^>]*)?>/`
Alex Barrett
+2  A: 

A PCRE equivalent to your ERE regular expression would be:

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

But as Jacco already noted you don’t need a regular expression at all as you want to replace a constant value.

Gumbo