Gary Li
Gary Li

Reputation: 411

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

Upvotes: 0

Views: 106

Answers (3)

alex
alex

Reputation: 490647

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

Use a DOM parser.

Upvotes: 0

Magicianeer
Magicianeer

Reputation: 2180

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.

Upvotes: 1

Eli
Eli

Reputation: 5630

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.

Upvotes: 0

Related Questions