Próbuję parsować niektóre HTML z DOM w PHP, ale mam pewne problemy. Po pierwsze, na wypadek, gdyby to zmieniło rozwiązanie, HTML, który mam, nie jest pełną stroną, a raczej jest tylko jego częścią.Jak analizować częściowy kod HTML?
<!-- This is the HTML that I have --><a href='/games/'>
<div id='game'>
<img src='http://images.example.com/games.gif' width='300' height='137' border='0'>
<br><b> Game </b>
</div>
<div id='double'>
<img src='http://images.example.com/double.gif' width='300' height='27' border='0' alt='' title=''>
</div>
</a>
Teraz próbuję uzyskać tylko element div o identyfikatorze double
. Próbowałem poniższy kod, ale wydaje się, że nie działa poprawnie. Co mogę robić źle?
//The HTML has been loaded into the variable $html
$dom=new domDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$keepme = $dom->getElementById('double');
$contents = '<div style="text-align:center">'.$keepme.'</a></div>';
echo $contents;
Co on robi lub nie robi? –