Re: Replace text inside html tags?
From: Bart Lateur (bart.lateur_at_pandora.be)
Date: 01/31/05
- Next message: Eric Bohlman: "Re: FAQ 4.16 How can I find the Julian Day?"
- Previous message: John W. Krahn: "Re: Foreign characters in file name - can't open()"
- In reply to: A. Sinan Unur: "Re: Replace text inside html tags?"
- Next in thread: Gunnar Hjalmarsson: "Re: Replace text inside html tags?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Eric Bohlman: "Re: FAQ 4.16 How can I find the Julian Day?"
- Previous message: John W. Krahn: "Re: Foreign characters in file name - can't open()"
- In reply to: A. Sinan Unur: "Re: Replace text inside html tags?"
- Next in thread: Gunnar Hjalmarsson: "Re: Replace text inside html tags?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|