How to use the PHP DOM extension loadHTML

I was asked to close some of the “dangling” HTML tags, I have to use the PHP DOM extension and load the HTML.

I try to look for textbooks for a while, read this page , try different things, but I can't seem to figure out how to use it to achieve what I want.

I have this line: <div><p>The quick brown <a href="">fox jumps...

I need to write a function that closes open HTML tags.

Just look for a starting point here. Usually I can understand what is happening pretty quickly.

+2
source share
4 answers

DOMDocument PHP DOMDocument:: loadHTML() DOMDocument:: normalizeDocument().

<?php
    $html = '<div><p>The quick brown <a href="">fox jumps';

    $DDoc = new DOMDocument();
    $DDoc->loadHTML($html);
    $DDoc->normalizeDocument();

    echo $DDoc->saveHTML();
?>

:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> 
<html><body><div><p>The quick brown <a href="">fox jumps</a></p></div></body></html> 

substr strpos html, , :

<?php
    $html = '<div><p>The quick brown <a href="">fox jumps';

    $DDoc = new DOMDocument();
    $DDoc->loadHTML($html);
    $DDoc->normalizeDocument();

    $html = $DDoc->saveHTML();

    # Remove Everything Before & Including The Opening HTML & Body Tags.
    $html = substr($html, strpos($html, '<html><body>') + 12);
    # Remove Everything After & Including The Closing HTML & Body Tags.
    $html = substr($html, 0, -14);

    echo $html;
?>
+2

, DOM , , , Tidy.

+2

, : DOM , .

:

  • ,
  • Delete all child nodes after this point
  • Truncate string
0
source

Source: https://habr.com/ru/post/1743305/


All Articles