1

I need to change in styles all occurencies of width and height attributes in table/td/tr/th tags.

For example,

<table width="500" height="235" style="color:#FF0000;">
<tr>
<td width="30" height="25" style="color:#FFFF00;">Hello</td>
<td>World</td>
</tr>
</table>

should become

<table style="color:#FF0000;width:500px;height:235px"> 
<tr>
<td style="color:#FFFF00;width:30px;height:25px">Hello</td>
<td>World</td>
</tr>
</table>

how can I do it ?

SimoneB
  • 532
  • 1
  • 6
  • 16
  • 4
    [Thou shalt not use regular expressions to meddle with (X)HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Linus Kleen Feb 10 '11 at 15:13
  • 7
    `style="red"` is major nonsense. Please clean up your example code. – ThiefMaster Feb 10 '11 at 15:13
  • 1
    @Linus [Thou shalt not link to that question because it is wrong](http://stackoverflow.com/questions/4231382/regular-expression-pattern-not-matching-anywhere-in-string/4234491#4234491) – Gordon Feb 10 '11 at 15:24
  • @Gordon I shall [link here next time then](http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html). – Linus Kleen Feb 10 '11 at 15:25
  • 1
    @Linus yes, that's better. Or link to *(shameless self plug ahead)* [Best methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Feb 10 '11 at 15:28
  • 1
    @Gordon I upvoted this answer and shall use it for further linking. – Linus Kleen Feb 10 '11 at 15:30

2 Answers2

4

Without using regular expression (i.e.: the proper way):

$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

$nodes = $xpath->query('//*[@width or @height]');

foreach ($nodes as $node) {
    $style = $node->getAttribute('style');
    $width = $node->getAttribute('width');
    $height = $node->getAttribute('height');
    $node->removeAttribute('width');
    $node->removeAttribute('height');

    $style = !empty($style) ? "$style;" : '';
    if (!empty($width)) $style .= "width:{$width}px;";
    if (!empty($height)) $style .= "height:{$height}px;";

    $node->setAttribute('style', $style);
}

Note: red is not a valid CSS attribute. You probably meant color: red or background-color: red, for which the following would convert... change:

$style = !empty($style) ? "$style;" : '';

to...

$style = !empty($style) ? "color: $style;" : '';
netcoder
  • 61,842
  • 17
  • 117
  • 139
1

OK, so this would be better suited to a parser, but if you can guarantee that order of the attributes, this may work...

preg_replace('/<(table|td|tr|th)\s+width="(\d+?)"\s+height="(\d+?)"/',
             '<$1 style="width: $2; height: $3"',
             $str);

I left out the stuff which didn't make sense, as ThiefMaster said in the comments.

Community
  • 1
  • 1
alex
  • 438,662
  • 188
  • 837
  • 957