PHP MeCard解码器或MeCard转换为VCard转换器

时间:2013-02-12 03:26:20

标签: php qr-code vcard

我正在研究QR码解析器,我想知道是否有人知道MeCard库或将MeCard转换为VCard的代码。如果没有,MeCard是否有官方规范文档?我知道NTT DoCoMo创建了它,但我找不到任何类型的RFC。

1 个答案:

答案 0 :(得分:2)

http://code.google.com/p/zxing/wiki/BarcodeContents,我在http://www.nttdocomo.co.jp/english/service/developer/make/content/barcode/function/application/addressbook/index.html找到了指向DoCoMo的MeCard规范的链接。这很简单,将它转换为带有函数调用的VCard应该是非常简单的。

==编辑==

我写了一点转换函数。如果将来有人想要代码,它就在下面:

private function MeCardtoVCard($mecard_text)
{
    // Useful References:
    // http://www.nttdocomo.co.jp/english/service/developer/make/content/barcode/function/application/addressbook/index.html
    // http://code.google.com/p/zxing/wiki/BarcodeContents
    // http://en.wikipedia.org/wiki/VCard
    // https://theqrplace.wordpress.com/2011/05/02/qr-code-tech-info-mecard-format/

    $vcard = '';

    if (stripos($mecard_text, "mecard") === 0) 
    {
        $mecard_text = str_replace("\n", "", $mecard_text); // Strip out newlines
        $mecard_text = substr($mecard_text,7); // Strip off the MECARD: header

        $lines = explode(";", $mecard_text);

        if (count($lines) > 0)
        {
            // Using Version 2.1 because it is the most commonly supported.
            $vcard = "BEGIN:VCARD\nVERSION:3.0\n"; 

            foreach($lines as $l)
            {
                $line_elements = explode(":",$l);

                if (count($line_elements) > 1)
                {
                    // Must split into two parts.  Not sure how DoCoMo escapes
                    // data that actually contains a ":", so for now we are presuming
                    // that the first token is the property name and all other elements 
                    // are the value

                    $property = $line_elements[0];
                    $value = implode(":", array_slice($line_elements,1));

                    if ($property != '' && $value != '')
                    {
                        if ($property == 'N')
                        {
                            // MeCards only support first and last name.
                            $tmp = explode(",",$value);
                            if (count ($tmp) == 1)
                            {   
                                $vcard .= "N:;" . $tmp[0] . "\n";
                            }
                            else 
                            {
                                $vcard .= "N:" . implode(";",explode(",",$value)) . "\n";
                            }
                        }

                        if ($property == 'TEL') 
                        {
                            // MeCard does not use card types, so we will presume all of them are type CELL
                            $vcard .= "TEL:" . $value . "\n";
                        }

                        if ($property == 'ADR') 
                        {
                            // MeCard: "The fields divided by commas (,) denote PO box, room number, house number, city, prefecture, zip code and country, in order."
                            // VCard: "...post office box; the extended address; the street address; the locality (e.g., city); the region (e.g., state or province); the postal code; the country name" See http://www.ietf.org/rfc/rfc2426.txt 3.2.1
                            $vcard .= "ADR:" . implode(";",explode(",",$value)) . "\n";
                        }

                        if (in_array($property, array('NOTE', 'BDAY', 'URL', 'NICKNAME')))
                        {
                            $vcard .= $property . ':' . $value . "\n";
                        }
                    }
                }
            }

            $vcard .= "END:VCARD\n";

            return $vcard;
        }
        else 
        {
            return false;
        }
    }       

    return false;
}