tags:

views:

26

answers:

2

Hi, I want to get contents of a URL and display only the links present on that page. Please help. Thanks

A: 

EDIT:

In both cases the URL needs to be the complete URL including the protocol, ie: http://google.com. If what you posted in your comment to the other answer below is accurate this is your first problem. However the error your getting about the function being undefined means that the required libs for SimpleHtml havent been loaded before you make the call. Are you getting nay errors from your require/include statements?

Also when updating the question its best to post actual code in an edit to your original question so it can benefit from formatting.

Easiest way is to use SimpleXML

$htmlDoc = new SimpleXmlElement($url, null, true);
$anchors = $hmtlDoc->xpath('//a[@href]');

foreach($anchors as $a) {
  $attr = $a->attributes();
  echo sprintf('<a href="%s">%s</a>', $attr['href'], $a);
}
prodigitalson
Thanks for the help. I have tried the code but i am getting XML parsing errors. Please help
Zohaib
+1  A: 

Use file_get_contents to get the contents, and using XPath via DOM or SimpleXML.

Cary Chow
if he has `allow_url_fopen` enabled theres no need to use `file_get_contents` with SimpleXml or the DOM unless he needs to handle the response, and if thats the case hes better off using CURL. Still +1 for raising the step :-)
prodigitalson
I am using<code><?php$html = file_get_html('http://www.google.com/');foreach($html->find('a') as $element) echo $element->href . '<br>'; ?></code>But it gives the error, call to undefined fucntion file_get_html
Zohaib
Thanks for the help. I have tried the code but i am getting XML parsing errors. Please help
Zohaib