qrcode-PHP MeCard解码器或MeCard到VCard转换器


qr code - PHP MeCard Decoder or MeCard to VCard Converter

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

来源http://code.google.com/p/zxing/wiki/BarcodeContents,我在DoCoMo的MeCard规范中找到了一个链接http://www.nttdocomo.co.jp/english/service/developer/make/content/barcode/function/application/addressbook/index.html.它非常简单,通过函数调用将其转换为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;
}