2011-08-07 44 views
0

嗨,我想解析vCard格式爲一個數組。用戶可以上傳vCard 2,1或vCard 3.0,我應該可以解析它。我只想把名稱在vCard中的電子郵件放到php數組中。在php中解析vCard

我試過vcardphp.sourceforge.net。

<?php 

require("vcard.php"); 

$cards = parse_vcards(file('sample.txt')); 
print_r($cards); 


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

未定義指數:編碼H:\ WWW \ vcardphp \ vcard.php上線146 說明:未定義指數:CHARSET在H:\ WWW \ vcardphp \ vcard.php線149

和給出的示例代碼不工作太多未定義的索引:錯誤

+0

你能接受我以前的答案,如果它是有益的http://stackoverflow.com/questions/6971412/exporting-and-impoting-vcard –

+0

@Lawrence Cherone等待更多無論如何謝謝 – aWebDeveloper

+2

-1用於腳本請求。顯示你已有的代碼並解釋你遇到的問題,我可能會重新考慮。 – Arjan

回答

0

這只是該http://vcardphp.sourceforge.net/樣品不符合給定的代碼工作。

看到添加的:if (!empty($n[*])) $tmp[] = $n[*];

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 (!empty($n[3])) $tmp[] = $n[3];  // Mr. 
     if (!empty($n[1])) $tmp[] = $n[1];  // John 
     if (!empty($n[2])) $tmp[] = $n[2];  // Quinlan 
     if (!empty($n[4])) $tmp[] = $n[4];  // Esq. 
     $ret = array(); 
     if (!empty($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; 
} 

並修改vcard.php解析函數從vbook.php第一 - 您可以修改代碼,使其工作(所以它不會對丟失的數據失敗以適應不具有預期的參數。

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]); 
      } 
      $encoding_defined = array_key_exists('ENCODING', $this->params); 
      if ($encoding_defined && $this->params['ENCODING'][0] == 'QUOTED-PRINTABLE') { 
       $this->_decodeQuotedPrintable($lines); 
      } 
      $charset_defined = array_key_exists('CHARSET', $this->params); 
      if ($charset_defined && $this->params['CHARSET'][0] == 'UTF-8') { 
       $this->value = utf8_decode($this->value); 
      } 
      return true; 
     } 
    } 
    return false; 
}