dtwr2012 2013-02-12 03:26
浏览 46
已采纳

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

I'm working on a QR code parser and I'm wondering if anyone knows of a MeCard library, or code that converts a MeCard to a VCard. If not, is there an official specification doc for MeCard out there? I know NTT DoCoMo created it, but I can't find any kind of RFC on it.

  • 写回答

1条回答 默认 最新

  • dongxin8392 2013-02-12 04:41
    关注

    From http://code.google.com/p/zxing/wiki/BarcodeContents, I found a link to DoCoMo's specification for MeCard, at http://www.nttdocomo.co.jp/english/service/developer/make/content/barcode/function/application/addressbook/index.html. It is pretty dirt simple and converting it to a VCard with a function call should be pretty trivial.

    == EDIT ==

    I wrote a little conversion function. Just in case anyone in the future wants the code, it is below:

    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("
    ", "", $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
    VERSION:3.0
    "; 
    
                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] . "
    ";
                                }
                                else 
                                {
                                    $vcard .= "N:" . implode(";",explode(",",$value)) . "
    ";
                                }
                            }
    
                            if ($property == 'TEL') 
                            {
                                // MeCard does not use card types, so we will presume all of them are type CELL
                                $vcard .= "TEL:" . $value . "
    ";
                            }
    
                            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)) . "
    ";
                            }
    
                            if (in_array($property, array('NOTE', 'BDAY', 'URL', 'NICKNAME')))
                            {
                                $vcard .= $property . ':' . $value . "
    ";
                            }
                        }
                    }
                }
    
                $vcard .= "END:VCARD
    ";
    
                return $vcard;
            }
            else 
            {
                return false;
            }
        }       
    
        return false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图