Dan
Dan

Reputation: 4140

PHP - Replacing ereg with preg

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.

Upvotes: 2

Views: 1319

Answers (2)

Jacco
Jacco

Reputation: 23759

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.

Upvotes: 10

Gumbo
Gumbo

Reputation: 655319

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.

Upvotes: 3

Related Questions