Search code examples
phpregexpcreposix-ere

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.


Solution

  • 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.