6

This is the code I am using:

include 'simple_html_dom.php';
$html = file_get_html('index.html');
echo $html->find('tr', 15);

This will find the row 15 of the table. What I want to do is to remove that row completely.

I have already tried

$html->find('tr', 15)=null; 

But that does not seem to work. I have tried finding the info on the SimpleHTMLDom documentation but it does no contain much information.

3 Answers 3

1

simple_html_dom does not seems to allow the deletion.

Try with this instead:

$html = new DOMDocument();
$html->loadHTMLFile('index.html');
$element = $html->getElementsByTagName('tr')->item(15);
$element->parentNode->removeChild($element);
Sign up to request clarification or add additional context in comments.

3 Comments

the script stops responding after i use your code.is it possible to do so with phpquery or any other library
Sorry, I've changed the line $html = new DOMDocument; by $html = new DOMDocument();, and load by loadHTMLFile. This should works, I've used that method several times.
hello friend,this still not removing anything can you show me the working example if possible
0

here you have a working example (works as is in Linux, but is easily adaptable).

File dom_test.php:

#!/usr/bin/php
<?php
    $html = new DOMDocument();
    $html->loadHTMLFile('index.html');
    $element = $html->getElementsByTagName('tr')->item(1);
    $element->parentNode->removeChild($element);

    echo $html->saveHTML();
?>

Where the index.html contains:

<html>
    <head></head>
    <body>
        <table>
            <tr><td> hi </td><td>there</td></tr>
            <tr>
                <td> HELLO </td>
                <td> there </td>
            </tr>
            <tr><td> hi </td><td>there</td></tr>
        </table>
    </body>
</html>

Put both files in the same directory and execute this in the console:

php dom_test.php

The output will appears without the "HELLO there" row.

I hope that helps you.

Comments

0

You can do this with simple_html_dom, just set the outertext to the value of innertext

foreach($html->find('div') as $div) {
    $div->outertext = $div->innertext;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.