Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I am using following code to get positions of start and end of html tags. Problem is HTML::Tagreader requires file as argument. But I have HTML as a sting in some variable. I dont want to create a file and delete it. Just for using this module. Can any one suggest better solution where I can use string instead of file

Note : Problem is HTML::TagReader does not allow string argument. I am only trying to get position of html tags using this module. Is there any better option?

use HTML::TagReader; my $filename = 'test2.html'; # Here instead of using this file I wan +t to do same thing using HTML as a string in some variable say $html_ +string = 'content of test2.html' my $p=new HTML::TagReader "$filename"; open(my $fh, '<', $filename) or die "Could not open file '$filenam +e' $!"; my %line_chars; my $line_number = 1; while (my $row = <$fh>) { if ($line_number > 1) { $line_chars{$line_number} = $line_chars{$line_number - +1} + length($row); } else { $line_chars{$line_number} = length($row); } $line_number++; } my @atags; my %atagrange; while(my ($tagOrText,$tagtype,$linenumber,$column)=$p->getbytoken($s +howerr)) { my $position; my $a_start_tag_pos; if ($linenumber > 1) { $position = $line_chars{$linenumber - 1} + $column; }#print "\ntagOrText:" . $tagOrText . "\ntagtype : " . $tagtype +. "\nline number :" . $linenumber . "\ncolumn : " . $column . "\npos +ition : " . $position . "\n"; if ($tagtype eq "a" or $tagtype eq '/a') { if ($tagtype eq "a") { push(@atags, $position); } else { $a_start_tag_pos = pop(@atags); $atagrange{$a_start_tag_pos} = $position; } } }

thanks in advance...


In reply to Better way of finding HTML tags positions in HTML string by phoenix007

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (6)
    As of 2019-11-11 22:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Strict and warnings: which comes first?



      Results (63 votes). Check out past polls.

      Notices?