I did bother... and IMVHO I think it could help out the OP.

Ok, I didn't post *the* solution, but you probably also know there's always more than one way to solve a problem in Perl. This one isn't maybe the easiest, or fastest, or most elegant one... but I still think it could help.

btw: you can always downvote me if you don't like what I said ;)

pussy ass code to proof (I hate proofing) I did bother:

#!/usr/bin/perl use Text::Balanced qw{extract_multiple extract_bracketed extract_delim +ited extract_tagged}; my $text = '<p>paragraph<b>BOLD text</p><a href=">>>>">link text</a><d +iv><p>Some new text <b>in bold and <i>cursive</b></p></div><p>test</p +>'; local $\ = $/; print "text= $text"; my @stack; my $error; @fields = extract_multiple($text, [ { Tag => sub { extract_tagged($_[0]) } }, { Brack => sub { extract_bracketed($_[0],'<>') + } }, ], undef, 0 ); foreach my $elem (@fields){ if(ref( $elem ) eq 'Tag'){ (print_stack(), $error--) if defined $error; print "OK Tag: ".$$elem; } elsif(ref $elem eq 'Brack'){ my ($otag) = $$elem =~ m#<([a-zA-Z0-9]+)>#; my ($ctag) = $$elem =~ m#</([a-zA-Z0-9]+)>#; add_to_stack($otag) if defined $otag; remove_from_stack($ctag) if defined $ctag; } else { $error = 1; } } sub print_stack { foreach my $tag (@stack){ print "not properly closed: ". $tag if defined $tag; } } sub add_to_stack { push @stack, shift; } sub remove_from_stack { my $tag = shift; foreach my $c (reverse 0 .. @stack) { delete $stack[$c] if($stack[$c] eq $tag ) } }
Gives as output:
text= <p>paragraph<b>BOLD text</p><a href=">>>>">link text</a><div><p> +Some new text <b>in bold and <i>cursive</b></p></div><p>test</p> not properly closed: b OK Tag: <a href=">>>>">link text</a> not properly closed: i OK Tag: <p>test</p>

to ask a question is a moment of shame
to remain ignorant is a lifelong shame


In reply to Re^3: Ensuring HTML is "balanced" by insaniac
in thread Ensuring HTML is "balanced" by skx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.