while ($data=~ /<td.*?>\s*?<center>/is) {
$data =~ s|(<td.*?>)\s*<center>|$1 align=center>|is;
}
while ($data=~ /<td.*?>\s*?<center>/is && $& !~/<td>/i) {
while ($data=~ m|<td.*?>(?!.*</td>)\s*?<center>|is) {
I'm trying to find <td>'s followed by <center> so I can turn them into <td align=center>
[edited by: phranque at 9:59 pm (utc) on Jun 21, 2010]
[edit reason] disabled graphic smileys ;) [/edit]
For example, I've got nested tables and some of the tables themselves are centered with <center></center>.
while ($data =~ m|<td.+?</td>|g)
HTML::TreeBuilder::XPath is a good tool to find all occurrences. For manipulating you will need to study HTML::Element.
<img src="" alt="look here >" /> while ($data =~ m|<td.+?</td>|g)
| |
Before: <html><head></head><body><table><tr><td valign="top" width="120"></td><td><center>centered!</center></td><td><center>also centered?</center></td></tr></table></body></html>
After: <html><head></head><body><table><tr><td valign="top" width="120"></td><td align="center">centered!</td><td align="center">also centered?</td></tr></table></body></html>
$html = q[cell-1 ]; $tagged = $html; $counter=0; while ($html =~ m| cell-2 |isg) { $counter++; ($TD[$counter] = $&) =~ s|( \s* (.*?)|$1 align=center>$2|isg; $TD[$counter] =~ s| \s*||isg; # Kill only the final before the , not any earlier 's within the$tagged =~ s|([^#]) # # |$TD[$counter]|is; } $html = $tagged;
.*?
.+?
.*?.+?
i'm not sure these are doing what you are intending in those regular expressions.
the first means "zero or more of any character followed by a question mark" and the second means "one or more of any character followed by a question mark".
What are you not understanding about the code I posted?
I think it's pretty straight forward and you'll understand most lines if you just read them as plain english.
(2) I don't know what it means when a subroutine doesn't have a name.
(3) I don't know what it means when a subroutine is embedded directly into a FOR loop.
$tree->look_down( # look down the DOM-tree of HTML elements
'_tag' => 'td', # look for td-tags
sub { # only find those td-tags that ...
$_[0]->descendants() > 0 && # have child elements
($_[0]->descendants())[0]->tag() eq 'center' # and the first child is a center-tag
}
)
(4) I don't know what $_[0] means, at least not in this context.
sub { # only find those td-tags that ...
return $_[0]->descendants() > 0 && # have child elements
($_[0]->descendants())[0]->tag() eq 'center' # and the first child is a center-tag
}
(5) I'm not sure what the -> notation means here. It's not something I've used, at least not for a long time.
$centertd->attr('align', 'center'); # make <td> to <td align="center">
$centertd->push_content( ($centertd->descendants())[0]->content_list() ); # copy all the childs of the
# td's first child (which is <center>) directly into the td
($centertd->descendants())[0]->detach(); # delete the now obsolete first child
<!--START HEADER-->
<table class="header"><tr><td>
<h1> a very important heading </h1>
</td></tr></table>
<!--END HEADER-->
<!--START TEXT-->
<table class="imageleft"><tr><td>
<img src="/images/myimage.jpg" alt="LOOK AT IT" width="264" height="250">
<p class="center"><small><i>that image is great</i></small></p>
</td></tr></table>