Jump to content

Get from HTML code table with pre-defined attributes and inserting it to div


iknowthatimnoobphp

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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 by iknowthatimnoobphp
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.