Re: Getting source from remote file
| From: | Michiel Sikma | Date: | Thu, 06 May 2010 09:25:39 +0000 |
| Subject: | Re: Getting source from remote file | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-304942@lists.php.net to get a copy of this message | ||
On 6 May 2010 10:55, Peter Lind <peter.e.lind@gmail.com> wrote:
>
>
> You could parse the document with the DOM classes. Load as a
> DOMDocument, then grab what's needed with the relevant methods (you
> can use xpath to single out your divs with the right classes).
> http://dk2.php.net/domdocument
>
>
>
Xpath is indeed the way to go. For example...
/* .. your code.. */
$data = file_get_contents($url);
$inDoc = new DOMDocument();$inDoc->preserveWhiteSpace =
false;@$inDoc->loadHTML($data);
$xpath = new DOMXPath($inDoc);$xlinks =
$xpath->query('//div[@class="b"]/a');for ($a = 0, $z =
$xlinks->length; $a < $z; ++$a) {
$link = $xlinks->item($a);
$href = $xlink->attributes->getNamedItem('href')->value;
}
To be safe, you should use an ID instead of a class, though.
Michiel