Escribí lo siguiente:
<?php
$str = "http://stackoverflow.com";
$DOM = new DOMDocument;
$DOM->loadHTML($str);
//get all H1
$items = $DOM->getElementsByTagName("h1");
//display all H1 text
for ($i = 0; $i < $items->length; $i++)
{
echo $items->item($i)->nodeValue . "<br/>";
}
?>
Y solo quería recuperar todos los elementos H1 del stackoverflow, pero no puedo hacer que funcione. Siempre que intente completar la variable $ str manualmente (por ejemplo: <h1>hello</h1><div><h1>hello2</h1></div>
) está funcionando. Pero cada vez que intento analizar contenido de otra página web no se hace nada ...
¡La ayuda sería aplaudida!
Respuestas
0 para la respuesta № 1$str = "http://stackoverflow.com";
$DOM = new DOMDocument;
$DOM->loadHTMLFile($str); // get html
echo $DOM->saveHTML(); echo html
$DOM->saveHTMLFile(FILE_NAME); save html to file