2017-01-04 5 views
0

私はちょうどここに抜粋掲載Sourceforgeのhttps://sourceforge.net/projects/vcardphp/files/vcardphp/vcardphp-1.1.2/からvCardのパーサでApple Macの連絡先vCardを正しく解析するにはどうすればよいですか?

を働いています:

vcard.phpを

function parse(&$lines) 
{ 
    $this->_map = null; 
    $property = new VCardProperty(); 
    while ($property->parse($lines)) { 
     if (is_null($this->_map)) { 
      if ($property->name == 'BEGIN') { 
       $this->_map = array(); 
      } 
     } else { 
      if ($property->name == 'END') { 
       break; 
      } else { 
       $this->_map[$property->name][] = $property; 
      } 
     } 
     // MDH: Create new property to prevent overwriting previous one 
     // (PHP5) 
     $property = new VCardProperty(); 
    } 
    return $this->_map != null; 
} 



function parse(&$lines) 
{ 
    while (list(, $line) = each($lines)) { 
     $line = rtrim($line); 
     $tmp = split_quoted_string(":", $line, 2); 
     if (count($tmp) == 2) { 
      $this->value = $tmp[1]; 
      $tmp = strtoupper($tmp[0]); 
      $tmp = split_quoted_string(";", $tmp); 
      $this->name = $tmp[0]; 
      $this->params = array(); 
      for ($i = 1; $i < count($tmp); $i++) { 
       $this->_parseParam($tmp[$i]); 
      } 
      if ($this->params['ENCODING'][0] == 'QUOTED-PRINTABLE') { 
       $this->_decodeQuotedPrintable($lines); 
      } 
      if ($this->params['CHARSET'][0] == 'UTF-8') { 
       $this->value = utf8_decode($this->value); 
      } 
      return true; 
     } 
    } 
    return false; 
} 

vbook.php

function parse_vcards(&$lines) 
{ 
    $cards = array(); 
    $card = new VCard(); 
    while ($card->parse($lines)) { 
     $property = $card->getProperty('N'); 
     if (!$property) { 
      return ""; 
     } 
     $n = $property->getComponents(); 
     $tmp = array(); 
     if ($n[3]) $tmp[] = $n[3];  // Mr. 
     if ($n[1]) $tmp[] = $n[1];  // John 
     if ($n[2]) $tmp[] = $n[2];  // Quinlan 
     if ($n[4]) $tmp[] = $n[4];  // Esq. 
     $ret = array(); 
     if ($n[0]) $ret[] = $n[0]; 
     $tmp = join(" ", $tmp); 
     if ($tmp) $ret[] = $tmp; 
     $key = join(", ", $ret); 
     $cards[$key] = $card; 
     // MDH: Create new VCard to prevent overwriting previous one (PHP5) 
     $card = new VCard(); 
    } 
    ksort($cards); 
    return $cards; 
} 

function print_vcard($card, $hide) 
{ 
    $names = array('N', 'FN', 'TITLE', 'TEL', 'EMAIL', 'URL', 'ADR', 'NOTE'); 

    $row = 0; 

    foreach ($names as $name) { 
     if (in_array_case($name, $hide)) { 
      continue; 
     } 
     $properties = $card->getProperties($name); 
     if ($properties) { 
      foreach ($properties as $property) { 
       $show = true; 
       $types = $property->params['TYPE']; 
       if ($types) { 
        foreach ($types as $type) { 
         if (in_array_case($type, $hide)) { 
          $show = false; 
          break; 
         } 
        } 
       } 
       if ($show) { 
        $class = ($row++ % 2 == 0) ? "property-even" : "property-odd"; 
        print_vcard_property($property, $class, $hide); 
       } 
      } 
     } 
    } 
} 


function print_vcard_property($property, $class, $hide) 
{ 
    $name = $property->name; 
    $value = $property->value; 

    $types = $property->params['TYPE']; 
    if ($types) { 
      print_r(array_filter($types)); 
      } 
    switch ($name) { 
     case 'N': 
      $name = $property->getComponents(); 
      print_r(array_filter($name)); 
     break; 
      case 'TEL': 
      $tel = $property->getComponents(); 
      print_r(array_filter($tel)); 
     break; 
     case 'FN': 
      $company = $property->getComponents(); 
      print_r(array_filter($company)); 
     break;  
     case 'ADR': 
      $adr = $property->getComponents(); 
      print_r(array_filter($adr)); 
      break; 
     case 'EMAIL': 
      $email = $property->getComponents(); 
      print_r(array_filter($email)); 
      break; 
     case 'URL': 
      $url = $property->getComponents(); 
      print_r(array_filter($url)); 
      break; 
     default: 
      $components = $property->getComponents(); 
      $lines = array(); 
      foreach ($components as $component) { 
       if ($component) { 
        $lines[] = $component; 
       } 
      } 
      $html = join("\n", $lines); 
      break; 
    } 
    echo "<br>"; 
    echo "<br><br>"; 

} 

LL私はこのようになりますサンプルvCardファイル、使用しているとき:

BEGIN:VCARD 
N:Smith;Jim;Alvin;Mr. 
FN:Jim A. Smith 
CATEGORIES:Family 
BDAY:1977-01-27 
ADR;WORK:;Suite 900;11 5th Street;Coco Beach;FL;32082 
ADR;HOME:;;198 Elm Street;Coco Beach;FL;32082 
TEL;WORK:904-555-9384; 
TEL;HOME:904-873-0394 
TEL;CELL:904-934-3429 
END:VCARD 

をしかし、Appleの連絡先からの私のvCardは少し異なります:私の例ではそう

BEGIN:VCARD 
N:Underwood;Frank;;; 
FN:Frank Underwood 
item1.EMAIL;type=INTERNET;type=pref:[email protected] 
item2.TEL;type=pref:+01 321 323123123 
item2.X-ABLabel:Mobil 
item3.ADR;type=pref:;;Richmondstreet 21;Washington D.C;;12312; 
item4.URL;type=pref:http://www.frankunderwood.com/ 
item4.X-ABLabel:_$!<HomePage>!$_ 
CATEGORIES:Friends 
END:VCARD 

の名前のみ電子メール、電話、住所またはURLではなく、人物が印刷されます。したがって、私はitem1、item2などを削除する必要があります。しかし、私はそれをどうやって行うのか分かりません。連絡先/設定/ vCardのに

移動し、2.1とUnicode(UTF-8)にフォーマットを変更:ここで

+0

あなたは同じスクリプト内で ''パース(と呼ばれる2つの函数を)持っているようです。それはタイプミスですか?そうでない場合、私はインタプリタからのいくつかのエラーメッセージを見ることになります。 – RiggsFolly

+0

@RiggsFollyはい、これはsourceforgeの元のコードからのものです。私のページの上部には 'error_reporting(E_ALL&〜E_NOTICE);がありますが、エラーは発生しません。 – Jarla

+0

' ini_set( 'display_errors'、1); 'を追加して、ページにエラーが表示されるようにしてください – RiggsFolly

答えて

0

を正しく解析することができ、適切なvCardファイルへのAppleの連絡先をエクスポートする方法ソリューションです

enter image description here

関連する問題