-1

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

Abu Romaïssae
  • 3,841
  • 5
  • 37
  • 59

1 Answers1

1

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.

Abu Romaïssae
  • 3,841
  • 5
  • 37
  • 59