Hi i want to parse vCard format to a array. User may upload vCard 2,1 or vCard 3.0 i should be able to parse it. I just want the email with names in the vCard in to a php array.
i have tried vcardphp.sourceforge.net.
<?php
require("vcard.php");
$cards = parse_vcards(file('sample.txt'));
print_r($cards);
function parse_vcards($lines)
{
$cards = array();
$card = new VCard();
while ($card->parse($lines)) {
$property = $card->getProperty('N');
if (!$property) {
return "";
}
$n = $property->getComponents();
$tmp = array();
if ($n[3]) $tmp[] = $n[3]; // Mr.
if ($n[1]) $tmp[] = $n[1]; // John
if ($n[2]) $tmp[] = $n[2]; // Quinlan
if ($n[4]) $tmp[] = $n[4]; // Esq.
$ret = array();
if ($n[0]) $ret[] = $n[0];
$tmp = join(" ", $tmp);
if ($tmp) $ret[] = $tmp;
$key = join(", ", $ret);
$cards[$key] = $card;
// MDH: Create new VCard to prevent overwriting previous one (PHP5)
$card = new VCard();
}
ksort($cards);
return $cards;
}
?>
Undefined index: ENCODING in H:\www\vcardphp\vcard.php on line 146 Notice: Undefined index: CHARSET in H:\www\vcardphp\vcard.php on line 149
and the sample code given doesnt work at all Too many Undefined index: errors
It's just that the http://vcardphp.sourceforge.net/ sample doesn't work with the given code. You can modify the code to make it work (so it doesn't fail on missing data - first from vbook.php:
See the added:
if (!empty($n[*])) $tmp[] = $n[*];
And modify the vcard.php parse function to accomodate not having the expected parameters.
I would take a look at the open source project vCard PHP. Has worked for me!
http://vcardphp.sourceforge.net/