phppreg-replaceereg-replace

ereg_replace code


i updated my php to 5.3 and i have a problem with my ereg_replace

$txt = ereg_replace("<(/)?(font|span|div|del|ins)[^>]*>","",$txt); 
$txt = ereg_replace("<([^>]*)(class|lang|style|size|face)=(\"[^\"]*\"|'[^']*'|[^>]+)([^>]*)>","<\\1>",$txt);
$txt = ereg_replace("<([^>]*)(class|lang|style|size|face)=(\"[^\"]*\"|'[^']*'|[^>]+)([^>]*)>","<\\1>",$txt);

if i change the ereg_replace to preg_replace i get the warning :

Message: preg_replace() [function.preg-replace]: Unknown modifier ']'

could someone help me fix the preg_replace


Solution

  • It is because you don't have pattern delimiters at all. You need to add them.

    Try this

    $pattern1 = '#<(/)?(font|span|div|del|ins)[^>]*>#';
    $pattern2 = '#<([^>]*)(class|lang|style|size|face)=(\"[^\"]*\"|'[^']*'|[^>]+)([^>]*)>#';
    $txt = preg_replace($pattern1, '', $txt);
    $txt = preg_replace($pattern2, '<\\1>', $txt);
    

    Better yet though, would be to not use regex at all to try to parse HTML like this.