php简单读取.vcf格式文件的方法示例

2019-11-13,,,,,

本文实例讲述了php简单读取.vcf格式文件的方法。分享给大家供大家参考,具体如下:

/**
* 读取.vcf格式文件
* @param $filename
*/
function readCvf($filename){
 $file = fopen($filename,"r");
 while(! feof($file))
 {
   $line=fgets($file);
   $encoding = mb_detect_encoding($line, array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'));
   $content = iconv($encoding, "utf-8", $line);
   $arr = explode(":",$content) ;
   if($arr[0]=="NOTE;ENCODING=QUOTED-PRINTABLE"){
   $temp= quoted_printable_decode($arr[1]);
   $encoding = mb_detect_encoding($temp, array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'));
   $arr[1] = iconv($encoding, "utf-8", $temp);
   }
   if(count($arr)==2){
    $userInfo[$arr[0]] = $arr[1] ;
   }
 }
 fclose($file);
 return $userInfo;
}

经常遇到乱码问题:解决方法两步:

$encoding = mb_detect_encoding($line, array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'));
$content = iconv($encoding, "utf-8", $line);

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》及《php字符串(string)用法总结》

希望本文所述对大家PHP程序设计有所帮助。

《php简单读取.vcf格式文件的方法示例.doc》

下载本文的Word格式文档,以方便收藏与打印。