A function among several others to parse a google results page, I wrote this some time ago - google has probably changed their site since then, but I thought this might be helpful to someone.
I'm moving servers, but I will probably throw this up on my blog when I get it back up.
<?php
function googleResult($listItem) {
$listItem = $listItem->childNodes;
foreach($listItem as $element) {
if(is_object($element) && get_class($element) == 'DOMElement' && $element->hasChildNodes()) {
$hrefContainer = $element->childNodes;
foreach($hrefContainer as $element2) {
if(is_object($element2) && get_class($element2) == 'DOMElement' && $element2->nodeName == 'a' && $element2->hasAttribute('href')) {
$anchor = $element2;
unset($h3);
unset($element2);
break;
} else {
}
}
unset($element);
unset($listItem);
break;
}
}
if(empty($anchor) || !is_object($anchor) || get_class($anchor) != 'DOMElement') {
return false;
}
$href = $anchor->getAttribute('href');
if(empty($href)) {
return false;
}
$description = $anchor->childNodes;
$urlDescription = '';
foreach($description as $words) {
$name = trim($words->nodeName);
if($name == 'em' || $name == '#text' || $name == 'b') {
if(!empty($words->nodeValue)) {
$text = trim($words->nodeValue);
$urlDescription = $urlDescription . $text . ' ';
}
}
}
$urlDescription = htmlspecialchars_decode($urlDescription, ENT_QUOTES);
$urlDescription = trim($urlDescription);
return array('description' => $urlDescription, 'href' => $href);
}