Search code examples
phpregexpreg-replacepreg-replace-callback

How can I replace the code with preg_replace_callback()? (/e)


$in = '(<(/?(?:strong|p|em|a|ol|ul|li|img|iframe)\b.*?)>)ie';

$contenu = preg_replace($in, "'<'.html_entity_decode('$1',ENT_QUOTES,'UTF-8').'>'", $contenu);

Solution

  • $pattern = '~&lt;(/?(?:strong|p|em|a|ol|ul|li|img|iframe)\b.*?)&gt;~is';
    
    $contenu = preg_replace_callback($pattern, function ($m) {
        return '<' . html_entity_decode($m[1], ENT_QUOTES, 'UTF_8') . '>'; }, $contenu);