您好我想将vCard格式解析为数组。用户可以上传vCard 2,1或vCard 3.0我应该能够解析它。我只想把vCard中名字的电子邮件发送到php数组。
我试过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;
}
?>
未定义的索引:第146行的H:\ www \ vcardphp \ vcard.php中的ENCODING 注意:未定义的索引:第149行的H:\ www \ vcardphp \ vcard.php中的CHARSET
并且给出的示例代码根本不起作用太多未定义的索引:错误
答案 0 :(得分:1)
我会看看开源项目vCard PHP。对我有用了!
答案 1 :(得分:0)
只是http://vcardphp.sourceforge.net/示例不适用于给定的代码。您可以修改代码以使其正常工作(因此在丢失数据时不会失败 - 首先来自vbook.php:
请参阅已添加的内容: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解析函数以适应没有预期参数。
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;
}