tags:

views:

164

answers:

3

I need replace some tags in HTML using Perl:

I have this:

<span class="a">text</span><span class="a">text</span><span id="b">text</span>

I need this, where the span tags with class=a are changed to b tags instead:

<b>text</b><b>text</b><span id="b">text</span>

I tried using HTML::Manipulator but did not succeed.

A: 

I would use HTML::Tree to parse the HTML, then find the nodes that have the properties you want, change them, then output the new tree, which will have the changes you desire.

David M
Thanks, I'm a low level user... Is there another easier option?
Victor Sanchez
If you want to stay a low level user, sure, there are probably easier, nastier shortcuts. I *could* explain to you how to use regular expressions to make the specific change you want, because it's easy to use regular expressions to do that. But I encourage you to take the opportunity to solve the general problem of modifying HTML programatically in such a way that nested tags don't confound the easier ways.
David M
+8  A: 

Here's how to use HTML::TreeBuilder:

use strict;
use warnings;
use HTML::TreeBuilder;

my $html_string = '<span class="a">text</span><span class="a">text</span><span id="b">text</span>';    

my $root = HTML::TreeBuilder->new_from_content($html_string);
$root->elementify;  # Make $root into an HTML::Element object;


for my $e ( $root->look_down( _tag => 'span', class => 'a' ) ) {
    $e->tag( 'b' );
    $e->attr( class => undef );
} 

print $root->as_HTML;
daotoad
Thank you for providing the example that I was too lazy to write.
David M
Thanks, this code is very simple... and work very good.
Victor Sanchez
+2  A: 

An example using HTML::Parser:

#! /usr/bin/perl

use warnings;
use strict;
use HTML::Parser;
my $p = HTML::Parser->new( api_version => 3,
  start_h => [\&start, "tagname, attr, text, skipped_text"],
  end_h   => [\&end,   "tagname,       text, skipped_text"],
);
$p->parse_file(\*DATA);

my @switch_span_end;
sub start {
  my($tag,$attr,$text,$skipped) = @_;
  print $skipped;
  unless ($tag eq 'span' && ($attr->{class}||"") eq "a") {
    print $text;
    return;
  }
  push @switch_span_end => 1;
  print "<b>";
}

sub end {
  my($tag,$text,$skipped) = @_;
  print $skipped;
  if (@switch_span_end && $tag eq "span") {
    print "</b>";
    pop @switch_span_end;
  }
  else {
    print $text;
  }
}
__DATA__
<span class="a">text</span><span class="a">text</span><span id="b">text</span>

Output:

<b>text</b><b>text</b><span id="b">text</span>
Greg Bacon
Thanks... this is very cool... but I preffer using HTML::TreeBuilder because is more clear.
Victor Sanchez