Skip to content

Instantly share code, notes, and snippets.

@sony-mathew
Last active February 20, 2018 17:39
Show Gist options
  • Save sony-mathew/a5470f5e89ec022083268078d9d17bd2 to your computer and use it in GitHub Desktop.
Save sony-mathew/a5470f5e89ec022083268078d9d17bd2 to your computer and use it in GitHub Desktop.
Phone Number Formatter. To format the phone contacts that is to add +91 or 0 to all contacts.
/*
Date : 29 Jan 2013
This php script is used to format the contacts in csv or vcf file.
For example you may when you out of state you can't call your friends without appending a zero or +91 (in INDIA) to all the numbers you dial.
It is very tiresome to edit each one of them.
Using this script it all of them are formatted in single second and it saves your time also.(only for India)
Follow the steps as follows:
***If you have multiple .vcf files , first step is to combine it into a single vcf file by doing the following.
1. Open command line interface (click Start > Run, or windows button + r and type cmd to launch it).
2. Navigate to the folder where multiple vCard files are by entering cd “D:\documents”, if the folder is your documents.
3. Enter command copy /a *.vcf combinedfile.vcf
*** Now you have the combined .vcf file.
*** Open the contacts_formatter.php file
*** if your input file name is not combinedfile.vcf change it accordingly in the 3rd line.
*** you can use .vcf or .csv formats but make sure the input and output file has same format that is either .csv or .vcf.
*** Run the PHP script in a server.To setup a local php server download xampp. run the apache server. Place the .php file in htdocs directory. And run it.
*/
<?php
$input_file_name = 'combinedfile.vcf';
$output_file_name = 'final.vcf';
function add_0($matches) {
$matches[0][0] = ':';
$temp = explode(':', $matches[0]);
return ',0'.$temp[1];
}
function replace_91_to_0($matches) {
$matches[0][0] = ':';
$temp = explode(':91', $matches[0]);
return ',0'.$temp[1];
}
function replace_plus91_to_0($matches) {
$matches[0][0] = ':';
$temp = explode(':+91', $matches[0]);
return ',0'.$temp[1];
}
$input = file_get_contents($input_file_name);
$pattern = "/,[6-9][0-9]{9},/";
$output = preg_replace_callback( $pattern, "add_0" , $input);
$pattern = "/,91[6-9][0-9]{9},/";
$output = preg_replace_callback( $pattern, "replace_91_to_0" , $output);
$pattern = "/,\+91[6-9][0-9]{9},/";
$output = preg_replace_callback( $pattern, "replace_plus91_to_0" , $output);
file_put_contents($output_file_name, $output);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment