读取vcf(vcard)标签和值

时间:2011-08-03 05:05:33

标签: php pear vcard vcf

我正在尝试读取vcf,我已经获取了值,但无法检索类型和前缀。 请参阅以下代码

<?php
require_once( 'Contact_Vcard_Parse.php' );
if ( $_FILES['file']['tmp_name'] )
{
  $parse = new Contact_Vcard_Parse();
  $cardinfo = $parse->fromFile( $_FILES['file']['tmp_name'] );
  foreach( $cardinfo as $card )
  {
    $first = $card['N'][0]['value'][0][0];
    $last = $card['N'][0]['value'][1][0];
    $email = $card['EMAIL'][0]['value'][0][0];
    $telcount=0;
    while($telcount<50)
    {       
      $teltitle = $card['TEL'][$telcount]['node'][0][0];
      $tel = $card['TEL'][$telcount]['value'][0][0];
      if(strlen($tel)>=10) 
      { 
        echo 'title : '.$teltitle.'&nbsp;&nbsp;TEL : '.$tel.'<br>'; 
      }
      $telcount++; 
    } 
?>
    email : <?php echo( $email ); ?>
    <br>n : <?php echo( $first ); ?> <br>fn : <?php echo( $last ); ?> <br>
<?php 
  } 
} 
?> 

2 个答案:

答案 0 :(得分:0)

https://github.com/nuovo/vCard-parser

我在一年之前就是这个vCard解析器。它的工作就像一个魅力。您可以在代码中使用此库。

PS:不要重新发明轮子.. :)

答案 1 :(得分:0)

如果您不想使用任何库:

$VArquivoApple = fopen("./contactsapple.vcf", 'r');
$VArrayApple = [];
while (($VLinha = fgetcsv($VArquivoApple)) !== false)
    $VArrayApple[] = $VLinha[0];
fclose($VArquivoApple);

$VAContatosApple = [];

for ($Vi = 0; $Vi < count($VArrayApple); $Vi++) {
    if ($VArrayApple[$Vi] == "BEGIN:VCARD")
        $VContatoApple = new stdClass();
    if ($VArrayApple[$Vi] != "BEGIN:VCARD" && $VArrayApple[$Vi] != "VERSION:3.0") {
        if ($VArrayApple[$Vi] != "END:VCARD") {
            $VAtributo = isset(explode(":", $VArrayApple[$Vi])[0]) ? explode(":", $VArrayApple[$Vi])[0] : "";
            $VValor = isset(explode(":", $VArrayApple[$Vi])[1]) ? explode(":", $VArrayApple[$Vi])[1] : "";
            $VContatoApple->{$VAtributo} = $VValor;
        } else
            $VAContatosApple[] = $VContatoApple;
    }
}

foreach ($VAContatosApple as $value) {
    var_dump($value);
}