Here's a quick, untested, stab at it. Let's assume for this example that you are talking about <p> tags.

use HTML::TokeParser::Simple; # assumes that $text is a scalar containing the actual HTML my $p = HTML::TokeParser::Simple->new( \$text ); my $token; do { $token = $p->get_token } until $token->is_start_tag('p'); my $new_text = $token->return_text; do ( $token = $p->get_token ) { my $temp = $token->return_text; if ( $token->is_text ) { $temp =~ s/\s+/ /g; # collapse whitespace $temp =~ s/^\s//; # remove initial whitespace $temp =~ s/\s$//; # remove trailing whitespace } $new_text .= $temp; } until $token->is_end_tag('p'); $new_text .= $token->return_text;

This is a much cleaner method (and accurate) method of accomplishing this task than most regex solutions. I also happen to think that HTML::TokeParser::Simple is easier to use than many other HTML parsing modules. Of course, I may be biased as I wrote that module :)

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to Re: taking white space out between closing and opening tags by Ovid
in thread taking white space out between closing and opening tags by chuleto1

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.