【问题标题】:PHP MeCard Decoder or MeCard to VCard ConverterPHP MeCard 解码器或 MeCard 到 VCard 转换器
【发布时间】:2013-02-13 01:19:53
【问题描述】:

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

【问题讨论】:

    标签: php qr-code vcf-vcard


    【解决方案1】:

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

    【讨论】:

    • 正确的 vCard 换行符序列是“\r\n”,而不是“\n”。此外,您应该转义 vCard 属性值中的所有逗号、分号和换行符(除非它们具有特殊含义,例如在 N 和 ADR 中)。
    • 例如:$vcard .= "N:;" . str_replace(";", "\\;", $tmp[0]) . "\r\n";
    • 感谢迈克尔的提示!我将更新我的代码并重新测试它,然后更新上面的示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 2011-10-25
    • 2011-07-11
    • 2023-03-18
    • 1970-01-01
    • 2016-06-19
    • 2021-12-10
    相关资源
    最近更新 更多