Remove current node from HTML and fetch the final HTML using DOMDocument php

DS9

I have a html like below:

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>ABC</td>
            <td><a data-permission="allow"></a></td>
        </tr>
        <tr>
            <td>B</td>
            <td><a data-permission="allow"></a></td>
        </tr>
        <tr>
            <td>C</td>
            <td><a data-permission="allow"></a></td>
        </tr>
        <tr>
            <td>D</td>
            <td><a data-permission="allow"></a></td>
        </tr>
        <tr>
            <td>E</td>
            <td><button type="button" data-permission="allow"></button></td>
        </tr>
    </tbody>
</table>

Now i am finding the nodes who contains "data-permission" attributes like (a, button etc.) from above example.
TO do that i am using the below code. Now what i am trying do is remove that whole <a>..</a> or <button>...</button> or any other element if they contain "data-permission" attribute and after deletion only return remaining HTML. So how to achieve that?

$dom = new DOMDocument;
$dom->loadHTML($output);

$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//@data-permission-id');
foreach ($nodes as $node) {
    echo $node->nodeValue;
    //$node->parentNode->removeChild($node); throws the error "Not Found Error"
}

Note- I have tried $node->parentNode->removeChild($node); inside loop, but it throws the error. Also after delete that tag, i want to get remaining HTML. I have read the How to delete element with DOMDocument? but it doesn't help.

Niklesh Raut

Replace your node value to remove : $node->nodeValue = "";

$dom = new DOMDocument;
$dom->loadHTML($output);
echo "Previous : ".PHP_EOL.$dom->textContent.PHP_EOL;
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//*[@data-permission='allow']");
foreach ($nodes as $node) {
    $node->nodeValue = "";
    $dom->saveHTML();
}

Live demo : https://eval.in/885719

Live demo with your table data : https://eval.in/885780

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove HTML Tag using DOMDocument

From Dev

Remove HTML Tag using DOMDocument

From Dev

Parsing HTML with PHP using DomDocument

From Dev

Getting data from HTML using DOMDocument

From Dev

PHP DomDocument HTML Manipulation

From Dev

Differentiating HTML and XML with PHP DomDocument

From Dev

How to get the html of certain tag using php DomDocument

From Dev

How to get the exact structure of an HTML document using DomDocument or XPath in PHP?

From Dev

Is it possible to split HTML using DOMDocument?

From Dev

Removing a div with specific id from a html string using domdocument

From Dev

Remove HTML Attributes using PHP

From Dev

How do you access an HTML node using DOMDocument while retaining the inner HTML formatting?

From Dev

How to remove both .php and .html extensions from url using NGINX?

From Dev

Remove <img> tag containing a word from Html string using PHP

From Dev

remove specific class from html string using php

From Dev

PHP/HTML ~> href link from mysql fetch

From Dev

How to remove a specific element from DOMDocument in PHP?

From Dev

Remove text from html node with jQuery/Javascript

From Dev

Using DOMDocument to create elements in an HTML file?

From Dev

Separate HTML, CSS, and JavaScript from file with DomDocument

From Dev

Fetch and display data from database in table format in HTML form using PHP

From Dev

Using PHP to fetch content from a DOM node but not from its children

From Dev

PHP - How to remove/trim HTML from Array?

From Dev

Remove all links from DOM html with PHP

From Dev

php Remove tag from html based on getAttribute

From Dev

How to remove html and php extensions from URL

From Dev

Remove .php from action link in html

From Dev

How to set new HTML tag in custom class that extends DOMElement (using DOMDocument in php)?

From Dev

How to remove current row from a html table in jQuery json?

Related Related

  1. 1

    Remove HTML Tag using DOMDocument

  2. 2

    Remove HTML Tag using DOMDocument

  3. 3

    Parsing HTML with PHP using DomDocument

  4. 4

    Getting data from HTML using DOMDocument

  5. 5

    PHP DomDocument HTML Manipulation

  6. 6

    Differentiating HTML and XML with PHP DomDocument

  7. 7

    How to get the html of certain tag using php DomDocument

  8. 8

    How to get the exact structure of an HTML document using DomDocument or XPath in PHP?

  9. 9

    Is it possible to split HTML using DOMDocument?

  10. 10

    Removing a div with specific id from a html string using domdocument

  11. 11

    Remove HTML Attributes using PHP

  12. 12

    How do you access an HTML node using DOMDocument while retaining the inner HTML formatting?

  13. 13

    How to remove both .php and .html extensions from url using NGINX?

  14. 14

    Remove <img> tag containing a word from Html string using PHP

  15. 15

    remove specific class from html string using php

  16. 16

    PHP/HTML ~> href link from mysql fetch

  17. 17

    How to remove a specific element from DOMDocument in PHP?

  18. 18

    Remove text from html node with jQuery/Javascript

  19. 19

    Using DOMDocument to create elements in an HTML file?

  20. 20

    Separate HTML, CSS, and JavaScript from file with DomDocument

  21. 21

    Fetch and display data from database in table format in HTML form using PHP

  22. 22

    Using PHP to fetch content from a DOM node but not from its children

  23. 23

    PHP - How to remove/trim HTML from Array?

  24. 24

    Remove all links from DOM html with PHP

  25. 25

    php Remove tag from html based on getAttribute

  26. 26

    How to remove html and php extensions from URL

  27. 27

    Remove .php from action link in html

  28. 28

    How to set new HTML tag in custom class that extends DOMElement (using DOMDocument in php)?

  29. 29

    How to remove current row from a html table in jQuery json?

HotTag

Archive