regex - PHP Find and replace function -



regex - PHP Find and replace function -

i need regular look find , replace i'm little stuck on.

basically, have table several <td>'s in each <td> have a<img>

i need re-create width , height <img> , place in <td>

i have been doing manually, number of <td>'s growing , taking long.

i need help doing actual find , replace

any ideas?

-- edit --

thanks advice, think go downwards dom route know better. have done reg ex before did task much more simple thought went there. you've saved me time

you should consider using domdocument class provided php.

example:

<?php $doc = new domdocument; $doc->loadhtmlfile('/path/to/file.html'); // find td elements. $tds = $doc->getelementsbytagname('td'); // if td elements found, loop through each 1 of them. if ( ! empty($tds) ) foreach ( $tds $td ) { // find img elements located within td $imgs = $td->getelementsbytagname('img'); // find style attribute of td in case 1 exists. $style = $td->getattribute('style'); // i'm not sure if multiple images found instead of looping many, create sure 1 found. if ( ! empty($imgs) && count($imgs) == 1 ) { $height = $imgs->item(0)->getattribute('height'); $width = $imgs->item(0)->getattribute('width'); if ( ! empty($height) ) $style .= 'height:' . $height . 'px;'; if ( ! empty($width) ) $style .= 'width:' . $width . 'px;'; // update style attribute of td element. $td->setattribute('style', $style); } } // save html document. $doc->savehtmlfile('/path/to/newfile.html');

updates:

<html> <body> <table> <tr> <td><img src="test.png" height="100" width="100" /></td> <td> <p><img src="test2.png" height="100" /></p> </td> </tr> </table> </body> </html>

to:

<!doctype html public "-//w3c//dtd html 4.0 transitional//en" "http://www.w3.org/tr/rec-html40/loose.dtd"> <html> <body> <table> <tr> <td style="height:100px;width:100px;"><img src="test.png" height="100" width="100"></td> <td style="height:100px;"> <p><img src="test2.png" height="100"></p> </td> </tr> </table> </body> </html>

php regex replace

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -