开发者

Display Malformed HTML "Safely?"

Let's say I have some HTML like this:

<ol><li>a knock at the door, I'll be back in a second

which I display as inline HTML within a div that I produce. Is there any way to stop the inner, malformed HTML from screwing up the alignment of the entire document that comes after the HTML?

I realize that intelligently parsing it is one option (since I do have it on the server side), but I'm l开发者_C百科ooking for a lighter solution.

Edit I know I could just replace the < and > with entities, but I want to keep the formatting (within reason).


Despite you looking for a 'lighter solution' than HTML parsing, you might want to consider using HTML Purifier or HTML Tidy, since that should take care of broken tags for you - that's essentially what they were made for.


Do you need the inline text to support HTML? If not, you could just strip all HTML tags, or replace <> with escapes. Or you could store the text on the server as Markdown or any other non-HTML language and generate good HTML on the fly.

Edit: If you really need HTML, I suggest that you run an XML validator on the HTML snippet. You don't actually need to look at the structure of the XML: if the snippet is well-formed then it's very unlikely that it will break the rest of the layout.


Replace < and > with ascii/unicode equivalents &#60; and &#62; OR &lt; and &gt; OR \u003c and \u003e

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜