Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Ensuring HTML is "balanced"

by spiritway (Vicar)
on Mar 06, 2006 at 20:37 UTC ( [id://534751]=note: print w/replies, xml ) Need Help??


in reply to Ensuring HTML is "balanced"

I'm not sure if this is what you want, but, using search terms "HTML tags" I found a couple of possibilities: HTML::TagUtil, and HTML::EasyTags. While they may not do exactly what you want, they may provide information about the tags for you to make the desired corrections to your input.

The problem, of couse, is that it's often not possible to know exactly where the tags were intended. For example: <b>Which <i>exact text was supposed to be italics?</b>. Where does the </i> tag go? In more complex text, it is likely that only the original author knows just where the tags should have been placed - if, in fact, s/he even knows.

Replies are listed 'Best First'.
Re^2: Ensuring HTML is "balanced"
by apotheon (Deacon) on Mar 06, 2006 at 22:13 UTC

    It's true that knowing where the tag should be closed can be a tricky guesstimation at best in many circumstances. There's a simple way to decide where to put the closing tag when in doubt, though: just stick it in the last possible place to have it nest properly. Thus, in your example, the </i> would be placed just before the </b>, like so:

    <b>Which <i>exact text was supposed to be italics?</i></b>

    While this may not give you exactly what the original poster intended, it does help to get your code to validate properly.

    print substr("Just another Perl hacker", 0, -2);
    - apotheon
    CopyWrite Chad Perrin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://534751]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2024-04-16 09:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found