Home > front end >  PHP function that removes anything else but the specified part of string
PHP function that removes anything else but the specified part of string

Time:08-10

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?

CodePudding user response:

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

CodePudding user response:

May this be the solution you are searching: https://www.php.net/manual/en/function.strip-tags.php

<?php
$striped = strip_tags($htmlString, '<table>');
?>
  •  Tags:  
  • php
  • Related