Skip to content

Instantly share code, notes, and snippets.

@tliff
Created January 10, 2013 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tliff/4500652 to your computer and use it in GitHub Desktop.
Save tliff/4500652 to your computer and use it in GitHub Desktop.
<?php
$data = '<div>blabla<h1><a href="http://www.example.com/blabla.php?id=12345" class=l>blub</a></h1><h2><a href="http://www.example.com/asfadfafadf.php?id=12345" class=l>blub</a></h2>sdfafgdghaghagh<h1><a href="http://www.example.com/fdgadfhfh.php?id=123dsfasdf45" class=l>blub</a>asdfasdfasdf</h1>';
$doc = new DOMDocument();
$doc->loadHTML($data);
$elements = $doc->getElementsByTagName('h1');
$link_targets = array();
foreach ($elements as $element){
foreach($element->childNodes as $child){
if(get_class($child) == 'DOMElement' && $child->hasAttribute("href")){
$link_targets[] = $child->getAttribute("href");
}
}
}
print_r($link_targets);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment