I have a string, which consists of any html elements. For example, I have this string:
$htmlString = '<p>Test</p>
<h2>Test2</h2>
<table>
<thead>
<tr>
<td>Header 1</td>
<td>Header 2</td>
</tr>
</thead>
<tbody>
<tr>
<td>Col 1</td>
<td>Col 2</td>
</tr>
</tbody>
</table>
<span>Test span </span>
';
As you can see, the string consists of <p>
, <h2>
, <table>
, <span>
tags, and it could also contain other html tags.
My question is, is there a way so that I can make the string remove all the other elements except the <table>
, rest assured that there are no other tags other than thead, tr, td, tbody
inside the table element?