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.