Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 954
Dan Avatar asked Jun 21 '26 02:06

Dan


2 Answers

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.

like image 81
Jacco Avatar answered Jun 23 '26 17:06

Jacco


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.

like image 45
Gumbo Avatar answered Jun 23 '26 19:06

Gumbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!