我已升级到PHP 5.3,我需要知道如何将这些标记转换为Preg_replace。
有什么想法吗?
$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);
答案 0 :(得分:1)
它应该是相同的,你只需要添加分隔符(可能是“/”,“〜”或“@”,你最喜欢的没有“)。在替换字符串中你必须使用”$ 1“而不是“\ 1”!
它看起来像这样:
$html = preg_replace("~<(/)?(font|span|del|ins)[^>]*>~","",$html);
$html = preg_replace("~<([^>]*)(class|lang|style|size|face)=(\"[^\"]*\"|'[^']*'|[^>]+)([^>]*)>~", "<$1>", $html);
/ edit:您可以在分隔符后添加“i”(不带“),因为标签可能用大写字母书写,”i“是一个代表”case-insensetive“的修饰符。
$html = preg_replace("~<(/)?(font|span|del|ins)[^>]*>~i","",$html);
$html = preg_replace("~<([^>]*)(class|lang|style|size|face)=(\"[^\"]*\"|'[^']*'|[^>]+)([^>]*)>~i", "<$1>", $html);
关于主题:在html4中你可能有这样的东西:
<tagname name="<">
这意味着通过过滤所有内容而不使用“&lt;”或“&gt;”,你的正则表达式不会触发那些标签!但它非常罕见。