Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP function that removes anything else but the specified part of string

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This will probably be closed as a duplicate, but before that happens here’s some quick code to help you with your specific HTML. Instead of “removing” everything except your target text, we are “extracting” our target text. The code itself is pretty straightforward so I didn’t see a need to comment things as much as I usually do.

<?php
$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>
';
$dom = new DOMDocument();
$dom->loadHTML($htmlString, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$dom->preserveWhiteSpace = true;
$tables = $dom->getElementsByTagName('table');
foreach($tables as $table) {
    var_dump($dom->saveHTML($table));
}

Demo here: https://3v4l.org/YjkdT

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading