[PHP] Getting source from remote file
Hi guys and girls, okay, this is a dumbnut question I wouldn't bother asking but I really did hit a spot now where I am totally wedged up in my head and can't think straight anymore... so the, I bet easy, answer to my question escapes me. What I am trying to do is the following: Read the contents of a remote file via file() So I do $content = file($url); Now $content holds the HTML source of the page. Now this page has some DIVs in it with a class myclass123 (just for this purpose) Now I want to get the content of these divs into a variable / some variables. No more, no less. Can anybody please direct me towards the answer? Damn, I must sleep more at night, so I can think straight at work Any help is very much appreciated! Thanks! Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Getting source from remote file
On 6 May 2010 10:47, Auto-Deppe C. Hänsel c.haen...@auto-deppe.de wrote: Hi guys and girls, okay, this is a dumbnut question I wouldn't bother asking but I really did hit a spot now where I am totally wedged up in my head and can't think straight anymore... so the, I bet easy, answer to my question escapes me. What I am trying to do is the following: Read the contents of a remote file via file() So I do $content = file($url); Now $content holds the HTML source of the page. Now this page has some DIVs in it with a class myclass123 (just for this purpose) Now I want to get the content of these divs into a variable / some variables. No more, no less. Can anybody please direct me towards the answer? Damn, I must sleep more at night, so I can think straight at work Any help is very much appreciated! Thanks! Chris 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 -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Getting source from remote file
On 6 May 2010 10:55, Peter Lind peter.e.l...@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('//d...@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
RE: [PHP] Getting source from remote file
On 6 May 2010 10:55, Peter Lind peter.e.l...@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('//d...@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 Hi all, and thanks a lot for your suggestions. It works well now. The only problem I do have are german Umlaute [äöü] when receiving the content of the remote page. It#s formatted in iso-8859-1 and I'd rather have it in UTF-8. But utf8_de/encode won't help me there, I'm afraid. Anyhow, thanks a lot for your help! Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Getting source from remote file
On 6 May 2010 14:20, Auto-Deppe C. Hänsel c.haen...@auto-deppe.de wrote: Hi all, and thanks a lot for your suggestions. It works well now. The only problem I do have are german Umlaute [äöü] when receiving the content of the remote page. It#s formatted in iso-8859-1 and I'd rather have it in UTF-8. But utf8_de/encode won't help me there, I'm afraid. http://dk2.php.net/manual/en/function.mb-convert-encoding.php might be of help. Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 /hype -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php