-2

How do I remove a div with a certain id from an external html file with PHP?

This is a part of index.php:

<div id="linkovi">text to be deleted</div>

I have made a PHP script that makes the same div with an id that changes every time I run it. Now I need a way to remove one of those divs. The real problem is that, if I want to delete one of these divs that is in between two other divs that I don't want to delete, like this:

<div id="linkovi">text to be deleted</div>
<div id="linkovi1">text to be deleted 1</div> <!-- Div that should be deleted-->
<div id="linkovi2">text to be deleted 2</div>

Is there any way of removing the div in between?

Leon Kunštek
  • 555
  • 1
  • 7
  • 21
  • Take a look at this link. I'm no expert in php, so I won't answer this question. However, someone else can look at this link and answer it. https://stackoverflow.com/questions/5045598/getting-elements-of-a-div-from-another-page-php – Aniket G Apr 15 '19 at 16:23
  • I have to ask, what are you doing with these HTML files? Why are you editing them? Are they purely for display? If so, why not just output the content dynamically with PHP? – waterloomatt Apr 15 '19 at 16:25
  • @AniketG This is not the anwser I am looking for. – Leon Kunštek Apr 15 '19 at 16:41
  • @waterloomatt Because I want to make a type of commenting system without the use of databases. – Leon Kunštek Apr 15 '19 at 16:42
  • @LeonKunštek I know. I just thought that link may help someone else develop an answer for you – Aniket G Apr 15 '19 at 16:42
  • @AniketG Thanks anyway. – Leon Kunštek Apr 15 '19 at 16:44
  • See @miken32's response if you want to start manipulating HTML. Don't use regular expressions - they'll lead you into a pit of despair. However, and this is important, there are much, much better ways to develop a commenting system than editing HTML files directly. You don't even have to use a database. If you could provide some _real requirements_, we could help. – waterloomatt Apr 15 '19 at 18:00
  • 1
    @waterloomatt that would be getting way too broad for this site. This is already a “how do I do it” question without any code. – miken32 Apr 15 '19 at 18:02
  • Agreed. @LeonKunštek - try to use one of the baked in XML processing engines to load the HTML, find the node, remove it, and return the final string. Post back here if you run into issues. – waterloomatt Apr 15 '19 at 18:11
  • What code do you need @waterloomatt? I will gladly post the code if it could help. – Leon Kunštek Apr 15 '19 at 19:00

3 Answers3

2

This works:

function deleteDivById($a,$b) {
    $x = file_get_contents($b);
    $y = '<did id="'.$a.'">';
    $dom = new DOMDocument;
    $dom->loadHTML($x);
    $xPath = new DOMXPath($dom);
    $nodes = $xPath->query('//*[@id="'.$a.'"]');
    if($nodes->item(0)) {
        $nodes->item(0)->parentNode->removeChild($nodes->item(0));
    }
    $z = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));
    echo $z;
 }

Usage:

 deleteDivById('linkovi1','htmlfile.html');

Hope this helps!

Seth B
  • 1,014
  • 7
  • 21
  • This answer kinda works but the PHP and the HTML are in separate files so when I run the PHP it loads the interface from index.php and while that version of the website doesn't have the div I wanted to remove it doesn't remove the actual div from the file and if I open index.php the div is still there. So it works but it doesn't actually remove the div from index.php. – Leon Kunštek Apr 15 '19 at 21:09
  • So you have the divs on the index.php not the HTML file? – Seth B Apr 15 '19 at 21:27
  • If you want to use Javascript, you can use the other answer. – Seth B Apr 15 '19 at 21:29
1

I made a function that actually finds and removes the <div> from the targeted file. This is the function:

function delete_div($file, $target, $replacement, $from, $to){
    $side = file($file, FILE_IGNORE_NEW_LINES);
    $reading = fopen($from, 'r');
    $writing = fopen($to, 'w');

    $replaced = false;

    while (!feof($reading)) {
        $line = fgets($reading);
           if (stristr($line,$target)) {
              $line = $replacement;
              $replaced = true;
           }
        fputs($writing, $line);
    }
    fclose($reading); fclose($writing);
    if ($replaced){
       rename($to, $from);
    } else {
       unlink($to);
    }
}

$file is the file in which you want to remove the div.

$target is the div or any other element you want to replace (in the $target you have to specify what is in the div, the closing and opening tags and the spaces, aka. ' ').

$replacement is what you want to replace the div with (in my case it's '' which means nothing).

$from is to rename the file (in the process, everything from the file is put into a temporary file and after the div is replaced, the file gets renamed back into it's original name. I know I could have just used the $file data instead of putting another parameter into the function, but I wrote it like that in case someone wants to rename the file after the div has been replaced).

$to is the name of the temporary file (It doesn't matter what you name the temporary file as long as you put the extension after the filename, because the temporary file gets deleted after the div has been replaced)

So you could use the function like this:

delete_div('index.php', '<div id="linkovi1">text to be deleted 1</div>', '', 'index.php', 'rip.tmp');
Leon Kunštek
  • 555
  • 1
  • 7
  • 21
0

Another solution:

function removeElements($elementSelectors) {

    // Create a javascript array of selectors
    $jsArray = '[ ';

    foreach($elementSelectors as $selector) {
        $jsArray .= "'" . $selector . "', ";
    }

    $jsArray = substr($jsArray, 0, -2); // remove last ","
    $jsArray .= ' ]';

    $script = '(() => ' . $jsArray . '.map(selector => document.querySelector(selector))'; // searches for the element
    $script .= '.filter(element => !!element).forEach(element => element.remove()))();';

    echo '<script>' . $script . '</script>';
}

By calling like this:

removeElements(array('#some-element', '#other-element'));

The output would be:

<script>(() => [ '#some-element', '#other-element' ].map(selector => document.querySelector(selector)).filter(element => !!element).forEach(element => element.remove()))();</script>

This php function creates an immediately invoked javascript function that will remove the elements given. This should work fine with IDs as selectors.

Paul Cosma
  • 303
  • 2
  • 11