如何在 Php 中创建具有多个联系人的 .vcf 文件

问题描述 投票:0回答:2

我想在单个文件中创建多个联系人的电子名片。因此,如果仅进行一次安装,我将能够导入所有联系人。

我能够通过单个联系人创建 .vcf 文件。 我如何生成具有多个联系人的相同内容。

我正在使用这个库

php vcf-vcard
2个回答
0
投票

这是我使用的代码。

$vcard = "";

while ($row = $result->fetch_assoc()) {
    // Get contact information from the database
    $name = $row['name'];
    $email = $row["email"];
    $phone = $row["wa_no"];

    // Split the name into individual words
    $nameParts = explode(' ', $name);

    // Get the last name
    $lastName = array_pop($nameParts);

    // Set the first name and middle name
    $firstName = '';
    $middleName = '';

    // If there are remaining name parts after removing the last name
    if (!empty($nameParts)) {
        // The first part is the first name
        $firstName = array_shift($nameParts);

        // The remaining parts (if any) are the middle name
        $middleName = implode(' ', $nameParts);
    }

    // Create a new vCard entry
    $vcard .= "BEGIN:VCARD\r\n";
    $vcard .= "VERSION:3.0\r\n";
    $vcard .= "N:$lastName;$middleName;$firstName;SL365 ;\r\n";
    $vcard .= "FN:$name-SL365\r\n";
    $vcard .= "TEL;type=CELL;waid=$phone:" . substr($phone, 0, 2) . " " . substr($phone, 2) . "\r\n";
    $vcard .= "EMAIL;TYPE=INTERNET:$email\r\n";
    $vcard .= "END:VCARD\r\n";
}

// Set the HTTP headers to download the vCard file
$today = date("YmdHis");
header("Content-Type: text/x-vcard");
header("Content-Disposition: inline; filename=SLcontacts$today.vcf");

// Output the vCard data
echo $vcard;

0
投票

是的,可以在 .vcf 文件中包含同一用户的多个电话号码。这是一种常见的做法,尤其是对于出于个人、工作、家庭等不同目的而使用不同电话号码的个人。

创建 .vcf 文件时,您可以包含多个电话号码字段,并为每个字段添加适当的标签。以下是具有四个电话号码的用户的 .vcf 文件中电话号码部分的示例结构:

使用此格式:

BEGIN:VCARD
VERSION:3.0
N:Lastname;Firstname;;;
FN:Firstname Lastname
TEL;TYPE=CELL:+1234567890
TEL;TYPE=WORK:+1234567891
TEL;TYPE=HOME:+1234567892
TEL;TYPE=OTHER:+1234567893
EMAIL:[email protected]
END:VCARD
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.