How do I extract links from an external webpage and display them with php?
Example:
I would have php extract all the links from: http://www.php-mysql-tutorial.com/
and display the links in the php file.
How do I extract links from an external webpage and display them with php?
Example:
I would have php extract all the links from: http://www.php-mysql-tutorial.com/
and display the links in the php file.
Sorry, but as far as I know of, you can't extract/open anything from an external webpage.
This would be the logical way to do it:
But you can't get contents from external pages... I'll keep looking, but I don't think it's possible.PHP Code:$page = file("http://www.php-mysql-tutorial.com/");
foreach ($page as $num => $lines) {
$links = strpos($lines,"<a");
$linksend = strpos($lines,"</a>");
echo substr($lines,$links,$linksend+3);
}
- Mike
Sure. Edit the search query:
PHP Code:$links = strpos($lines,"<a href=\"subfolder here\\");
- Mike
For a more robust solution, the PHP5 DOM module is capable of parsing HTML. You can then use getElementsByTagName() to find all the links in a document.
If you have PHP4, you'll need to use php-html. Then,You'll then have a nice list of URLs in $urls.Code:<?php include('htmlparser.inc'); $urls = array(); HtmlParser remotePage = new HtmlParser(file_get_contents('http://www.example.com/some/page.html')); while($remotePage->parse()) if($remotePage->iNodeType == NODE_TYPE_ELEMENT && strtolower($remotePage->iNodeName) == "a" && isset($remotePage->iNodeAttributes['href'])) array_push($urls, $remotePage->iNodeAttributes['href']); ?>You will, however, need to have allow_url_fopen enabled in your php.ini.But you can't get contents from external pages...
/EDIT: XML_HTMLSax is also an option. Probably a better one, since it's been accepted into PEAR.
Last edited by Twey; 01-29-2007 at 09:08 PM.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Wow... there's alot of PHP functions there that are DOM related![]()
- Mike
whoops. wrong topic. sorry
Last edited by jacksont123; 02-03-2007 at 10:20 PM.
Bookmarks