Parsing vCard in php

2019-06-09 14:25发布

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

标签: php vcard
2条回答
相关推荐>>
2楼-- · 2019-06-09 14:44

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[*];

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 (!empty($n[3])) $tmp[] = $n[3];      // Mr.
        if (!empty($n[1])) $tmp[] = $n[1];      // John
        if (!empty($n[2])) $tmp[] = $n[2];      // Quinlan
        if (!empty($n[4])) $tmp[] = $n[4];      // Esq.
        $ret = array();
        if (!empty($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;
}

And modify the vcard.php parse function to accomodate not having the expected parameters.

function parse(&$lines)
{
    while (list(, $line) = each($lines)) {
        $line = rtrim($line);
        $tmp = split_quoted_string(":", $line, 2);
        if (count($tmp) == 2) {
            $this->value = $tmp[1];
            $tmp = strtoupper($tmp[0]);
            $tmp = split_quoted_string(";", $tmp);
            $this->name = $tmp[0];
            $this->params = array();
            for ($i = 1; $i < count($tmp); $i++) {
                $this->_parseParam($tmp[$i]);
            }
            $encoding_defined = array_key_exists('ENCODING', $this->params);
            if ($encoding_defined && $this->params['ENCODING'][0] == 'QUOTED-PRINTABLE') {
                $this->_decodeQuotedPrintable($lines);
            }
            $charset_defined = array_key_exists('CHARSET', $this->params);
            if ($charset_defined && $this->params['CHARSET'][0] == 'UTF-8') {
                $this->value = utf8_decode($this->value);
            }
            return true;
        }
    }
    return false;
}
查看更多
我只想做你的唯一
3楼-- · 2019-06-09 14:48

I would take a look at the open source project vCard PHP. Has worked for me!

http://vcardphp.sourceforge.net/

查看更多
登录 后发表回答