in reply to Extracting a substring of N chars ignoring embedded HTML

I'd use a module to strip the HTML. Here's a quick try:
use HTML::TokeParser::Simple; my $doc = qq(story on GettingIt about <a href="http://ss.gettingit.com +/cgi-bin/gx.cgi/AppLogic+FTContentServer?GXHC_gx_session_id_FutureTen +seContentServer=7f12a816fa48a5b9&pagename=FutureTense/Demos/GI/Templa +tes/Article_View&parm1=A1545-1999Oct12&topframe=true">hacking polls</ +a>. Contrary to what the article says, Time is <b>not</b> checking fo +r multiple votes on <a href="javascript:document.timedigital.submit() +;">their poll</a>. And I'm happy to report that despite the fact that + my cheater scripts aren't running, I'm still beating Bill Gates.); my $doc2; my $total = 0; my $p = HTML::TokeParser::Simple->new( \$doc ); while ( my $token = $p->get_token ) { if ($token->is_text) { if (length($token->return_text) + $total <= 200) { $doc2 .= $token->return_text; $total += length($token->return_text); } else { for (split / /, $token->return_text) { if ($total + length($_) <= 200) { $doc2 .= $_ . ' '; $total += length($_) + 1; } else { last; } } chop($doc2) if $doc2 =~ /\s$/; } } else { $doc2 .= $token->as_is; } } print $doc2;
Prints
story on GettingIt about <a href="http://ss.gettingit.com/cgi-bin/gx.c +gi/AppLogi c+FTContentServer?GXHC_gx_session_id_FutureTenseContentServer=7f12a816 +fa48a5b9&p agename=FutureTense/Demos/GI/Templates/Article_View&parm1=A1545-1999Oc +t12&topfra me=true">hacking polls</a>. Contrary to what the article says, Time is + <b>not</b > checking for multiple votes on <a href="javascript:document.timedigi +tal.submit ();">their poll</a>. And I'm happy to report that despite the fact tha +t my cheat er scripts
HTH

Update: changed < limit to <= limit

Update: strike that last one -- misunderstood the question. here's try #2! :) fixes welcome!

--
"To err is human, but to really foul things up you need a computer." --Paul Ehrlich

Replies are listed 'Best First'.
Re: Re: Extracting a substring of N chars ignoring embedded HTML
by graff (Chancellor) on Jan 12, 2003 at 04:29 UTC
    Given the two updates, LTjake certainly has the approach I would take, but I think the version I saw would end up scanning the entire input post, rather than quiting as soon as the output string is done. Here's the way the while loop was written when I first saw it (with my commentary added):
    while ( my $token = $p->get_token ) { if ($token->is_text) { if (length($token->return_text) + $total <= 200) { $doc2 .= $token->return_text; $total += length($token->return_text); } else { for (split / /, $token->return_text) { if ($total + length($_) <= 200) { $doc2 .= $_ . ' '; $total += length($_) + 1; } else { last; ## THIS ONLY EXITS THE FOR LOOP } ## So this block runs over the } ## entire remainder of the post chop($doc2) if $doc2 =~ /\s$/; } } else { $doc2 .= $token->as_is; } }
    The solution would be to add the length test to the while loop condition, or else figure a way to avoid an inner for loop, so that "last" will really finish things off. And some other nit-picks:
    • Rather than calling the same "return_text" method so many times, I'd rather use it once to set a local variable (and having just read the TokeParser::Simple docs, it seems that "return_text" is depricated anyway -- use "as_is" in all cases).
    • The for loop uses a split on single space -- not what the OP intended -- but this leads to a question about the OP's stated goals: Would you want every input string of /\s{1,}/ to be represented (and counted) as a single space in the output string? (Myself, I would think "yes".)

    So here's my version of LTjake's while loop (not tested):

    while ( my $token = $p->get_token ) { my $tkntext = $token->as_is; $tkntext =~ s/\s+/ /g; # normalize all whitespace if ($token->is_text) { if (length($tkntext) + $total <= 200) { $doc2 .= $tkntext; $total += length($tkntext); } else { my $maxlen = 200 - $total; $doc2 .= substr( $tkntext, 0, rindex( $tkntext, ' ', $maxl +en ); last; # this finishes the while loop } } else { $doc2 .= " $tkntext "; } }
      Ah yes! Good catch graff. The big problem, as you noted was that I was parsing the whole text. Simply adding another last after the chop(...) fixes that. The other was splitting on one space -- normalizing whitespace like you've done fixes that. Lastly, calling return text all those times is really bad like you've noted, so i just assigned it to a var (and used as_is). Now, while i still like yours better, here's an updated version of mine :)
      while ( my $token = $p->get_token ) { if ($token->is_text) { my $text = $token->as_is; $text =~ s/\s+/ /g; if (length($text) + $total <= 200) { $doc2 .= $text; $total += length($text); } else { for (split / /, $text) { if ($total + length($_) <= 200) { $doc2 .= $_ . ' '; $total += length($_) + 1; } else { last; } } chop($doc2) if $doc2 =~ /\s$/; last; } } else { $doc2 .= $token->as_is; } }
      Thanks for the feedback!

      Minor fix to yours:
      $doc2 .= substr( $tkntext, 0, rindex( $tkntext, ' ', $maxlen );
      Should be:
      $doc2 .= substr( $tkntext, 0, rindex( $tkntext, ' ', $maxlen ) );
      it was missing a bracket. =)

      --
      "To err is human, but to really foul things up you need a computer." --Paul Ehrlich
Re: Re: Extracting a substring of N chars ignoring embedded HTML
by FamousLongAgo (Friar) on Jan 11, 2003 at 23:46 UTC
    You misunderstood my question. I am not interested in stripping the HTML, I want to keep it in the string. I just don't want the HTML tags to be counted as characters when I try to create a substring of length N.