Instantly share code, notes, and snippets.

Embed
What would you like to do?
CSV to VCF - Convert file for import Contacts
<?php
$csv_file = __DIR__."/contacts.csv";
/**
* Structure of the CSV file
* "Title","First Name","Middle Name","Last Name","Suffix","Company","Department","Job Title","Business Street","Business Street 2","Business Street 3","Business City","Business State","Business Postal Code","Business Country/Region","Home Street","Home Street 2","Home Street 3","Home City","Home State","Home Postal Code","Home Country/Region","Other Street","Other Street 2","Other Street 3","Other City","Other State","Other Postal Code","Other Country/Region","Assistant's Phone","Business Fax","Business Phone","Business Phone 2","Callback","Car Phone","Company Main Phone","Home Fax","Home Phone","Home Phone 2","ISDN","Mobile Phone","Other Fax","Other Phone","Pager","Primary Phone","Radio Phone","TTY/TDD Phone","Telex","Account","Anniversary","Assistant's Name","Billing Information","Birthday","Business Address PO Box","Categories","Children","Directory Server","E-mail Address","E-mail Type","E-mail Display Name","E-mail 2 Address","E-mail 2 Type","E-mail 2 Display Name","E-mail 3 Address","E-mail 3 Type","E-mail 3 Display Name","Gender","Government ID Number","Hobby","Home Address PO Box","Initials","Internet Free Busy","Keywords","Language","Location","Manager's Name","Mileage","Notes","Office Location","Organizational ID Number","Other Address PO Box","Priority","Private","Profession","Referred By","Sensitivity","Spouse","User 1","User 2","User 3","User 4","Web Page"
*/
$vcf_file = __DIR__."/contacts.vcf";
$oper_error = array(
"not_file" => "The file \"%s\" is not writable",
"not_open" => "Cannot open file (%s)",
"not_write" => "Cannot write to file (%s)",
"writed" => "Success, wrote contacts to file (%s)"
);
/**
* Convert a comma separated file into an associated array.
* The first row should contain the array keys.
*
* Example:
*
* @param string $filename Path to the CSV file
* @param string $delimiter The separator used in the file
* @return array
* @link http://gist.github.com/385876
* @author Jay Williams <http://myd3.com/>
* @copyright Copyright (c) 2010, Jay Williams
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
function csv_to_array($filename='', $delimiter=',')
{
if (!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
function write_into_file($filename='', $flcontent='') {
$d_esp = "";
$vcf_text = "BEGIN:VCARD
VERSION:2.1
N:%1\$s;%2\$s;;;
FN:%3\$s
TEL;CELL:%4\$s
END:VCARD\n";
// Let's make sure the file exists and is writable first.
if (!file_exists($filename) || !is_writable($filename)) {
$d_esp = "not_file";
} else {
// Opening $filename in append mode.
// The file pointer is at the bottom of the file.
if (!$handle = fopen($filename, 'a+')) {
$d_esp = "not_open";
} else {
$dissen = "";
if (!empty($flcontent) && is_array($flcontent)) {
foreach ($flcontent as $key => $content) {
$the_number = str_replace(" ", "", $content['Mobile Phone']);
$dissen .= sprintf($vcf_text, $content['First Name'], $content['Last Name'], $content['First Name']." ".$content['Last Name'], $the_number);
}
}
if (!empty($dissen))
// Write $dissen to our opened file.
$d_esp = (fwrite($handle, $dissen) === FALSE) ? "not_write" : "writed";
fclose($handle);
}
}
return $d_esp;
}
$disal = $derek = "";
$remod = csv_to_array($csv_file);
if (!empty($remod) && is_array($remod))
$derek = write_into_file($vcf_file, $remod);
if (!empty($derek))
$disal = sprintf($oper_error[$derek], $vcf_file);
echo $disal;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment