名字部分待优化,请谨慎使用!!!
/**
* 联系人去重
*/
public function actionUnique()
{
$file = 'in.vcf';
$newFile = 'out.vcf';
$end = 'END:VCARD';
$curName = $curPhone = $card = $all = '';
$numAll = [];
$cardAll = [];
$fp = @fopen($file, "r");
if ($fp) {
while (($buffer = fgets($fp)) !== false) {
if (substr($buffer, 0, 15) == 'N;CHARSET=UTF-8') {
$arr = explode(';', $buffer);
$curName = $arr[2];
}
if (substr($buffer, 0, 9) == 'TEL;CELL:') {
$phoneLine = $buffer;
$curPhone = str_replace(' ', '', substr($buffer, 9));
echo $curPhone . PHP_EOL;
}
if (trim($buffer) == $end) {
if (trim($curPhone) != '' && !in_array($curPhone, $numAll)) {
// if (1) {
$numAll[] = $curPhone;
$cardAll[] = 'BEGIN:VCARD' . PHP_EOL
. 'VERSION:2.1' . PHP_EOL
. 'N;CHARSET=UTF-8:;' . $curName . ';;;' . PHP_EOL
. 'FN;CHARSET=UTF-8: 勇哥' . PHP_EOL
. $phoneLine
. $end;
}
}
}
if (!feof($fp)) {
echo "Error: unexpectedfgets() fail! " . PHP_EOL;
}
fclose($fp);
echo 'cardCount:' . count($numAll) . PHP_EOL;
$uniCardAll = array_unique($numAll);
echo 'uniAll:' . count($uniCardAll) . PHP_EOL;
file_put_contents($newFile, join(PHP_EOL, $cardAll));
}
}