vCard 文件联系人去重处理

名字部分待优化,请谨慎使用!!!

/**
     * 联系人去重
     */
    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));
        }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值