开发者

PHP strip odd html tag

I am facing a problem开发者_运维百科 when I use strip_tags to convent HTML to text.

The html code is

<img style="max-width: 60px; max-height: 90px;
            width: expression(this.width > 60 ? 60: true);
            height: expression(this.height > 90 ? 90: true);"
     src="image.php?s=d377256dd97b17e9bf0b1182743c95c2&amp;u=1&amp;dateline=1215813557"
     alt="DailyFX Forum Administrator's Avatar" />

the strip_tags can't work well, I want write some code using preg_replace, but I don't how to match the last >, not the > in the style . Can you help me ?

Thanks

Gary


Since your markup is invalid you must sanitize it before using strip_tags or any other markup parser. For this specific issue, you can try: preg_replace("expression([^)]+)", "", $your_html)

I recommend you switch to using a stylesheet instead of inline styles so you have valid markup.


You really don't want to try to parse complicated HTML with a preg_replace. It's nearly impossible to get right.

Take a look at http://simplehtmldom.sourceforge.net/ or one of the other PHP HTML libraries.


Here is a perfect example where a regex won't cut it (at least one that isn't convoluted).

Use a DOM parser.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜