merge and convert vCard files to CSV

Issue

I need to convert a couple of contacts (vCard) files to a single CSV file using PHP

I’ve already tried this script in nodejs: https://gist.github.com/sriranggd/738325

also, I don’t want to use an online solution nor install any software, so I was wondering if I could find a script that I understand to run it locally preferable in PHP or NodeJS

Solution

After trying a couple of scripts on internet (php & nodejs) I decided to create my own small script, which takes the contact name from the file name, the search for phone number with a simple regex.

<?php
$files = scandir( './contacts' );
$fp = fopen( "./contacts.csv", 'w' );

foreach ($files as $contact) {
    if( !preg_match( "/\.vcf$/i", $contact ) ) continue;

    $name = str_replace( ".vcf", "", $contact );
    if( empty( $name ) ) continue;

    $content = file_get_contents( "./contacts/$contact" );

    preg_match_all( "/\+?\d+/i", $content, $m); 

    $row = array($name, end( $m[0] ) );
    fputcsv($fp, $row);
}

fclose($fp);
echo "Done";

This script is very limited, but I wanted to share it since it can easily adapted for other usages.

I hope it helps somebody some day.

Answered By – Abu Romaïssae

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published