iknowthatimnoobphp Posted October 28, 2015 Share Posted October 28, 2015 Hello,I need get from HTML code tables with specific class and width attribute and insert it into the div element. How do this with preg_replace? The following example shows what I want to do.Input Code: <table class="some-class" width="30%" cellspacing="0" cellpadding="0" border="0" style="color:red""> <tbody> <tr><td><img src="picture.jpg" /></td></tr> <tr><td>Lorem ipsum dolor sit amet</td></tr> </tbody> </table> <table class="some-class" cellspacing="0" cellpadding="0" border="0" width="100%" > <tbody> <tr><td><img src="picture.jpg" /></td></tr> <tr><td>Lorem ipsum dolor sit amet</td></tr> </tbody> </table> <table cellspacing="0" cellpadding="0" border="0" width="30%" class="some-class" > <tbody> <tr><td><img src="picture.jpg" /></td></tr> <tr><td>Lorem ipsum dolor sit amet</td></tr> </tbody> </table> Get tables with class= "some-class" and width="30%" and put it to the <div>.The order of the attributes table is not fixed and may be different (random). Output Code: <div> <table class="some-class" width="30%" cellspacing="0" cellpadding="0" border="0" style="color:red""> <tbody> <tr><td><img src="picture.jpg" /></td></tr> <tr><td>Lorem ipsum dolor sit amet</td></tr> </tbody> </table> </div> <table class="some-class" cellspacing="0" cellpadding="0" border="0" width="100%" > // does not insert to div because another attribute width <tbody> <tr><td><img src="picture.jpg" /></td></tr> <tr><td>Lorem ipsum dolor sit amet</td></tr> </tbody> </table> <div> <table cellspacing="0" cellpadding="0" border="0" width="30%" class="some-class" > <tbody> <tr><td><img src="picture.jpg" /></td></tr> <tr><td>Lorem ipsum dolor sit amet</td></tr> </tbody> </table> </div> Of course, the sample code is highly simplified. Tables can be included in other tables or HTML elements. Quote Link to comment https://forums.phpfreaks.com/topic/298889-get-from-html-code-table-with-pre-defined-attributes-and-inserting-it-to-div/ Share on other sites More sharing options...
Ch0cu3r Posted October 28, 2015 Share Posted October 28, 2015 No you do not want to be using regex for this. You want to parse the HTML document using PHP DOM and with the use of xPath queries for finding the HTML element(s) from a page. You may find SimpleHTMLDOM easier to use as it uses css selector queries (similar to JQuery). Quote Link to comment https://forums.phpfreaks.com/topic/298889-get-from-html-code-table-with-pre-defined-attributes-and-inserting-it-to-div/#findComment-1524586 Share on other sites More sharing options...
iknowthatimnoobphp Posted October 28, 2015 Author Share Posted October 28, 2015 (edited) Yea, you are right. I can't do this on regex. I'm tied of this My code is more complicated. Input code: <table width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#ffffff"> <tbody> <tr> <td style="padding: 0px; text-align: left;"> <table class="class-table-1" width="42%" cellspacing="0" cellpadding="0" border="0" align="right" style="max-width: 250px;"> <tbody> <tr> <td align="right" valign="top"><img src="image1.jpg" /></td> </tr> <tr> <td align="left">Lorem Ipsum</td> </tr> </tbody> </table> <table class="class-table-2" width="56%" cellspacing="0" cellpadding="0" border="0" align="left" style="max-width: 325px;"> <tbody> <tr> <td align="left"><img src="image2.jpg" /></td> </tr> </tbody> </table> </td> </tr> <tr> <td align="center" valign="top"><img src="image3.gif" /></td> </tr> </tbody> </table> I want to get tables [class=class-table-1" width="42%] and [class=class-table-2" width="56%] and insert into new table cells Output code: <table width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#ffffff"> <tbody> <tr> <td style="padding: 0px; text-align: left;"> <table dir="rtl"><tr><td width="250" align="right"> //<-- INSERTED <table class="class-table-1" width="42%" cellspacing="0" cellpadding="0" border="0" align="right" style="max-width: 250px;"> <tbody> <tr> <td align="right" valign="top"><img src="image1.jpg" /></td> </tr> <tr> <td align="left">Lorem Ipsum</td> </tr> </tbody> </table> </td><td width="330" align="left"> //<-- INSERTED <table class="class-table-2" width="56%" cellspacing="0" cellpadding="0" border="0" align="left" style="max-width: 325px;"> <tbody> <tr> <td align="left"><img src="image2.jpg" /></td> </tr> </tbody> </table> </td><tr></table> //<-- INSERTED </td> </tr> <tr> <td align="center" valign="top"><img src="image3.gif" /></td> </tr> </tbody> </table> I don't know how do this with simpleHTMLDOM Edited October 28, 2015 by iknowthatimnoobphp Quote Link to comment https://forums.phpfreaks.com/topic/298889-get-from-html-code-table-with-pre-defined-attributes-and-inserting-it-to-div/#findComment-1524588 Share on other sites More sharing options...
iknowthatimnoobphp Posted October 28, 2015 Author Share Posted October 28, 2015 OK. It is not hard as I thought ... I can do it Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/298889-get-from-html-code-table-with-pre-defined-attributes-and-inserting-it-to-div/#findComment-1524599 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.