[uf-discuss] Parsing XFN in PHP

Ciaran McNulty mail at ciaranmcnulty.com
Thu Apr 10 06:01:05 PDT 2008


On Thu, Apr 10, 2008 at 1:40 PM, Mark Ng <mark at markng.me.uk> wrote:
>  XFN itself is fairly easy to deal with by just throwing pages through
>  tidy and using DOM/SAX/xPath, surely ?  I made a rudimentary parser to
>  do this some time ago.  The code is a little ugly to publish, but I
>  don't mind sharing privately.

Here's a *very* hacky code example from when I just wanted to check my
'me' links - I include it here just to demonstrate how simple XFN can
be and hopefully it's apparent how easy it would be to work up into a
nice objecty system for spidering:

<?php

$url = 'http://ciaranmcnulty.com/';
if($html = @file_get_contents($url)){
	$dom = new DomDocument();
	if(@$dom->loadHtml($html)){
		$xpath = new DomXpath($dom);
		if($nodes = $xpath->query("//a[contains(concat(' ',
normalize-space(@rel), ' '),' me ')]")){
			foreach($nodes as $node){
				echo $node->getAttribute('href'), PHP_EOL;
			}
		}
	}
	else{ echo 'Could not parse HTML', PHP_EOL; }
}
else{  echo 'Could not fetch file', PHP_EOL; }
?>


More information about the microformats-discuss mailing list