douyan2470 2017-01-04 09:35
浏览 103
已采纳

如何正确解析Apple Mac联系人vCard?

I am working with the vCard Parser from Sourceforge https://sourceforge.net/projects/vcardphp/files/vcardphp/vcardphp-1.1.2/

I just post an excerpt here:

vcard.php

function parse(&$lines)
{
    $this->_map = null;
    $property = new VCardProperty();
    while ($property->parse($lines)) {
        if (is_null($this->_map)) {
            if ($property->name == 'BEGIN') {
                $this->_map = array();
            }
        } else {
            if ($property->name == 'END') {
                break;
            } else {
                $this->_map[$property->name][] = $property;
            }
        }
        // MDH: Create new property to prevent overwriting previous one
        // (PHP5)
        $property = new VCardProperty();
    }
    return $this->_map != null;
}



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]);
            }
            if ($this->params['ENCODING'][0] == 'QUOTED-PRINTABLE') {
                $this->_decodeQuotedPrintable($lines);
            }
            if ($this->params['CHARSET'][0] == 'UTF-8') {
                $this->value = utf8_decode($this->value);
            }
            return true;
        }
    }
    return false;
}

and

vbook.php

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;
}

function print_vcard($card, $hide)
{
    $names = array('N', 'FN', 'TITLE', 'TEL', 'EMAIL', 'URL', 'ADR', 'NOTE');

    $row = 0;

    foreach ($names as $name) {
        if (in_array_case($name, $hide)) {
            continue;
        }
        $properties = $card->getProperties($name);
        if ($properties) {
            foreach ($properties as $property) {
                $show = true;
                $types = $property->params['TYPE'];
                if ($types) {
                    foreach ($types as $type) {
                        if (in_array_case($type, $hide)) {
                            $show = false;
                            break;
                        }
                    }
                }
                if ($show) {
                    $class = ($row++ % 2 == 0) ? "property-even" : "property-odd";
                    print_vcard_property($property, $class, $hide);
                }
            }
        }
    }
}


function print_vcard_property($property, $class, $hide)
{
    $name = $property->name;
    $value = $property->value;

    $types = $property->params['TYPE'];
    if ($types) {
            print_r(array_filter($types));
            }
    switch ($name) {
        case 'N':
            $name = $property->getComponents();
            print_r(array_filter($name));
        break;
            case 'TEL':
            $tel = $property->getComponents();
            print_r(array_filter($tel));
        break;
        case 'FN':
            $company = $property->getComponents();
            print_r(array_filter($company));
        break;    
        case 'ADR':
            $adr = $property->getComponents();
            print_r(array_filter($adr));
            break;
        case 'EMAIL':
            $email = $property->getComponents();
            print_r(array_filter($email));
            break;
        case 'URL':
            $url = $property->getComponents();
            print_r(array_filter($url));
            break;
        default:
            $components = $property->getComponents();
            $lines = array();
            foreach ($components as $component) {
                if ($component) {
                    $lines[] = $component;
                }
            }
            $html = join("
", $lines);
            break;
    }
    echo "<br>";
    echo "<br><br>";

}

It works quite well when I am using the sample vcard file, which looks like this:

BEGIN:VCARD
N:Smith;Jim;Alvin;Mr.
FN:Jim A. Smith
CATEGORIES:Family
BDAY:1977-01-27
ADR;WORK:;Suite 900;11 5th Street;Coco Beach;FL;32082
ADR;HOME:;;198 Elm Street;Coco Beach;FL;32082
TEL;WORK:904-555-9384;
TEL;HOME:904-873-0394
TEL;CELL:904-934-3429
END:VCARD

But my vcard from Apple Contacts looks a little bit different:

BEGIN:VCARD
N:Underwood;Frank;;;
FN:Frank Underwood
item1.EMAIL;type=INTERNET;type=pref:frank.underwood@hoc.com
item2.TEL;type=pref:+01 321 323123123
item2.X-ABLabel:Mobil
item3.ADR;type=pref:;;Richmondstreet 21;Washington D.C;;12312;
item4.URL;type=pref:http://www.frankunderwood.com/
item4.X-ABLabel:_$!<HomePage>!$_
CATEGORIES:Friends
END:VCARD

So in my example only the name of the person is printed, not the email, phone, address or url. So in the parsing I need to remove item1,item2 and so on. But I do not know how to do that.

  • 写回答

1条回答 默认 最新

  • douyang5943 2017-01-04 11:14
    关注

    Here is a solution how to export the Apple Contacts into a proper vCard file, that can be parsed correctly:

    Go to Contacts/Settings/vCard and change the format to 2.1 and Unicode (UTF-8)

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了