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 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂