【问题标题】:Parsing vCard in php在 php 中解析 vCard
【发布时间】:2011-08-07 08:05:49
【问题描述】:

您好,我想将 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 中的编码 注意:未定义索引:第 149 行 H:\www\vcardphp\vcard.php 中的 CHARSET

给出的示例代码根本不起作用太多未定义的索引:错误

【问题讨论】:

  • 如果对我之前的回答有帮助,你能接受吗stackoverflow.com/questions/6971412/…
  • @Lawrence Cherone 无论如何都在等待更多答案,谢谢
  • -1 用于脚本请求。展示你已经拥有的代码并解释你遇到了什么问题,我可能会重新考虑。
  • 您会收到Undefined index 通知,因为 vcard 代码使用索引而不检查它们是否存在。您可以降低错误级别以隐藏这些消息,但这不是最好的主意。修复代码以便不触发这些消息会更好。

标签: php vcf-vcard


【解决方案1】:

我会看看开源项目 vCard PHP。对我有用!

http://vcardphp.sourceforge.net/

【讨论】:

  • @Lee Armstrong:谢谢!这对我的小项目很有效。
【解决方案2】:

只是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;
}

【讨论】:

    猜你喜欢
    • 2011-03-26
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多