Difference between revisions of "representative-hcard-parsing"

From Microformats Wiki
representative-hcard-parsing
Jump to navigation Jump to search
(added some draft hKit code for parsing representative-hcards)
(updated the code to use source syntax - highlighting! woohoo! thx BenWard!)
Line 16: Line 16:
 
It's experimental and quite 'alpha', so I'd suggest you test it and make adjustments as necessary.
 
It's experimental and quite 'alpha', so I'd suggest you test it and make adjustments as necessary.
  
<code><pre>&lt;?php
+
<source lang="php">
 +
<?php
  
 
// example hKit code to extract a representative hCard
 
// example hKit code to extract a representative hCard
Line 26: Line 27:
 
$result = $hkit->getByURL('hcard', $HTTP_GET_VARS['url']);
 
$result = $hkit->getByURL('hcard', $HTTP_GET_VARS['url']);
 
if (count($result) != 0) {
 
if (count($result) != 0) {
&nbsp;&nbsp;if (count($result) == 1) {
+
  if (count($result) == 1) {
&nbsp;&nbsp;&nbsp;&nbsp;$repcard = $result[0];
+
    $repcard = $result[0];
&nbsp;&nbsp;} else {
+
  } else {
&nbsp;&nbsp;&nbsp;&nbsp;foreach ($result as $card) {
+
    foreach ($result as $card) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (array_search($HTTP_GET_VARS['url'], $card) == true || $card['uid'] == $HTTP_GET_VARS['url']) {
+
      if (array_search($HTTP_GET_VARS['url'], $card) == true || $card['uid'] == $HTTP_GET_VARS['url']) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$repcard = $card;
+
        $repcard = $card;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
+
      }
&nbsp;&nbsp;&nbsp;&nbsp;}
+
    }
&nbsp;&nbsp;}
+
  }
 
}
 
}
 
print_r($repcard);
 
print_r($repcard);
?&gt;</pre></code>
+
?></source>
  
 
== see also ==
 
== see also ==

Revision as of 17:44, 17 November 2008

representative hCard parsing

Assuming you are already using code that properly implements hCard parsing, this page documents how to determine a representative hCard for a page using the current representative-hcard-brainstorming proposal.

url uid source

After parsing the hCard(s) on a page, if there is one which has a "url" property whose value is the url of the page (source) and is also a "uid" property for the hCard, then that is the representative hCard for the page.

url and rel me

If the aforementioned "url uid source" didn't find a representative hCard, then look for an hCard with a "url" property that also has the rel="me" relation. If you find such an hCard then you have found a representative hCard for the page.

Draft hKit (PHP5) code

I - (Tom) - have been working on implementing represenative hCard parsing in PHP so that a user can simply type in a URL pointing to a page with an hCard on it, and have their name and some details automatically filled in the form. The code below implements the following:

  1. If there is only one hCard on a page, it uses that.
  2. If there is more than one hCard on a page, it looks through to see if any of the cards have UID or URL fields that match the URL it searches, then selects that one if one exists.

It's experimental and quite 'alpha', so I'd suggest you test it and make adjustments as necessary.

<?php

// example hKit code to extract a representative hCard
// tom morris <http://tommorris.org>
// public domain 2007

include("hkit/hkit.class.php5");
$hkit = new hKit;
$result = $hkit->getByURL('hcard', $HTTP_GET_VARS['url']);
if (count($result) != 0) {
  if (count($result) == 1) {
    $repcard = $result[0];
  } else {
    foreach ($result as $card) {
      if (array_search($HTTP_GET_VARS['url'], $card) == true || $card['uid'] == $HTTP_GET_VARS['url']) {
        $repcard = $card;
      }
    }
  }
}
print_r($repcard);
?>

see also