Convert phone book from PBX to VCF / vCard

Telephone systems with an internal phone book allow easy dialing and recognition of callers on the connected system phones and within the system manufacturer’s software. But what to do if the phonebook with a few hundred contacts is to be transferred to another system which uses the VCF / vCard standard to import the data? To convert the Agfeo AS phonebook to VCF the following script can be used. The export of the phonebook from the phone system is following this schema:

"005"	"John Doe"	"0123456789"	"geschäftlich/Festnetz"	"1"	"0"	"005"	"1"
"005"	"John Doe"	"0123344556"	"geschäftlich/Fax"	"0"	"0"	"185"	"0"
"006"	"Max Muster"	"01701234567"	"geschäftlich/Mobil"	"1"	"0"	"009"	"1"
"006"	"Max Muster"	"01122334455"	"geschäftlich/Festnetz"	"0"	"0"	"074"	"0"

In the first column there is a kind of ID for the respective contact. So the same IDs belong to the same contact. The numbers in the last four columns probably define the default phone number of the contact and assign the contact in the system. These four columns are not needed here.

The goal was to convert these contacts into a .vcf or vCard file to import them into an OwnCloud. As a .vcf file the contacts of the Agfeo AS system can also be transferred to Outlook and to many other programs.

I wrote a PHP script for this, which reads the records, merges them and outputs a single VCF file with all contacts. The script can thus be executed directly on a PHP-enabled web server and uses as input the file export.txt from the same directory. My export file was ISO-8859-1 encoded, so no UTF-8 encoding is used for reading in here.

//Converts Agfeo AS Telefonbuch to VCF
function file_get_contents_utf8($fn) {
    $content = file_get_contents($fn);
    return mb_convert_encoding($content, 'UTF-8',
        mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));

$file = file_get_contents_utf8("export.txt");
$arrayLines = explode("\n", $file);

$contacts = array();
foreach ($arrayLines as &$line) {
    preg_match_all('"([^\\"]+)"', $line, $results);
    $contacts[] = array(
        "id" => $results[0][0],
        "name" => $results[0][2],
        "number" => $results[0][4],
        "type" => $results[0][6]

$IDs_unique = [];
foreach($contacts as $value) {
    if(!in_array($value["id"], $IDs_unique)) {
        $IDs_unique[] = $value["id"];

foreach($IDs_unique as $id) {
    $key = array_search($id, array_column($contacts, 'id')); //gibt Key zu ID
    $name = explode(",",$contacts[$key]["name"]);
    echo "BEGIN:VCARD\nVERSION:3.0\nN:".$name[0].";".$name[1].";;;\n";
    if (strlen($name[1]) > 1)
        echo "FN:".$name[1]." ".$name[0]."\n";
        echo "FN:".$name[0]."\n";

    foreach($contacts as $contact) {
        if ($contact["id"] == $id) {

            if (strcmp($contact["type"], "geschäftlich/Mobil") == 0 || strcmp($contact["type"], "privat/Mobil") == 0)
            echo "TEL;TYPE=CELL:".$contact["number"]."\n";

            if (strcmp($contact["type"], "geschäftlich/Festnetz") == 0)
                echo "TEL;TYPE=WORK:".$contact["number"]."\n";

            if (strcmp($contact["type"], "privat/Festnetz") == 0)
                echo "TEL;TYPE=HOME:".$contact["number"]."\n";

            if (strcmp($contact["type"], "geschäftlich/Fax") == 0 || strcmp($contact["type"], "privat/Fax") == 0)
                echo "TEL;TYPE=FAX:".$contact["number"]."\n";

    echo "END:VCARD\n\n";

The script outputs pure vCard format as a result, which can thus be copied to a single file and transferred directly to another system.

N:John Doe;;;;
FN:John Doe

N:Max Muster;;;;
FN:Max Muster

Leave a Comment