-1

Possible Duplicate:
replace ereg_replace with preg_replace

I've upgraded to PHP 5.3 and I need to know how to convert these tags to Preg_replace.

Any ideas?

$html = ereg_replace("<(/)?(font|span|del|ins)[^>]*>", "", $html);

// then run another pass over the html (twice), removing unwanted attributes    
$html = ereg_replace("<([^>]*)(class|lang|style|size|face)=(\"[^\"]*\"|'[^']*'|[^>]+)([^>]*)>", "<\\1>", $html);
$html = ereg_replace("<([^>]*)(class|lang|style|size|face)=(\"[^\"]*\"|'[^']*'|[^>]+)([^>]*)>", "<\\1>", $html);
Community
  • 1
  • 1
Snow_Mac
  • 5,727
  • 17
  • 54
  • 80

1 Answers1

1

It should be the same, you only have to add the delimiter (might be "/","~" oder "@", what you like most without the "). In the replacementstring you have to use "$1" instead of "\1"!

it would look like this:

$html = preg_replace("~<(/)?(font|span|del|ins)[^>]*>~","",$html);

$html = preg_replace("~<([^>]*)(class|lang|style|size|face)=(\"[^\"]*\"|'[^']*'|[^>]+)([^>]*)>~", "<$1>", $html);

/edit: you might add "i" (without ") after the delimiter because tags might be written in capital letters, "i" is a modifier which stands for "case-insensetive".

$html = preg_replace("~<(/)?(font|span|del|ins)[^>]*>~i","",$html);

$html = preg_replace("~<([^>]*)(class|lang|style|size|face)=(\"[^\"]*\"|'[^']*'|[^>]+)([^>]*)>~i", "<$1>", $html);

Something off topic: in html4 you might have something like this:

<tagname name="<"> 

which means by filtering everything without "<" or ">", your regex wont trigger with those tags! But its quite rare.

Mohammer
  • 405
  • 3
  • 15