Re: Replace text inside html tags?

From: Bart Lateur (bart.lateur_at_pandora.be)
Date: 01/31/05


Date: Mon, 31 Jan 2005 09:32:53 GMT

A. Sinan Unur wrote:

>I had never used HTML::TokeParser::Simple, so I gave that a shot:

>my $p = HTML::TokeParser::Simple->new(string => $html);
>
>my $in_font_tag;
>
>while(my $token = $p->get_token) {
> if($token->is_start_tag('font')) {
> print $token->as_is;
> $in_font_tag = 1;
> next;
> }
> if($token->is_end_tag('font')) {
> print $token->as_is;
> $in_font_tag = 0;
> next;
> }
> if($in_font_tag and $token->is_text) {
> my $text = $token->as_is;
> $text =~ s/HI/BYE/g;
> print $text;
> next;
> }
> print $token->as_is;
>}

I like to use ".." in code with this kind of functionality. This shows
IMO an aspect where a tokeparser approach is vastly superior to raw
usage of HTML::Parser.

    while(my $token = $p->get_token) {
        if($token->is_start_tag('font') .. $token->is_end_tag('font')) {
            if($token->is_text) {
                my $text = $token->as_is;
                $text =~ s/HI/BYE/g;
                print $text;
                next;
            }
        }
        print $token->as_is;
    }

-- 
	Bart.


Relevant Pages

  • Re: I really hate .NET especially inside Delphi
    ... functionality is not an option. ... What about breaking compatibility? ... IMO, it's an elegant-ish hack...but I wouldn't call it ... *better* than how other .NET languages do it. ...
    (borland.public.delphi.non-technical)
  • RE: workshhet variable
    ... IMO, INDIRECT's functionality offers the most straight-forward way ... to string up the concatenated bits and get it working, ...
    (microsoft.public.excel.worksheet.functions)
  • Re: A simple question for Abstract Class
    ... IMO, the "abstract" doesn't add any safety, it just reduces functionality. ... Hmm - for a novice programmer who doesn't know what he/she is doing, ...
    (comp.lang.java.programmer)
  • Re: OT - apple - brand loyalty
    ... product out there no matter what they turn their hand too IMO. ... Apple products of my own. ... I've got a Nano and I can't see that functionality is anything special - I ...
    (uk.media.tv.misc)
  • Re: Difference betweent DTS and SSIS
    ... but I guess the major one (imo) would be ... that SSIS provides real "T" functionality as opposed to DTS which was ... SSIS is also significantly faster in most cases. ...
    (microsoft.public.sqlserver.dts)