Example : 888-888-8888
I’m not sure why you wrote this question more than once… but at any rate.
My suggestion would be to use a logic hook, before save, that formatted the phone number the way you wanted it. For example, you could strip out any white space, dashes, and parentheses, then add your dashes after the first 6 characters, then after the first 3 characters… and Bob’s your Uncle!
You’ll have to decide what to do if you get more or less than 10 numbers or an international number. One option would be to leave any numbers alone that start with the + symbol, and then educate staff to use a + in front of international numbers.
sieberta
Thank you.
I created HOOK to automatic calculate and write data (difference between two dates field).
I created the following php code to use regex to convert any North American phone number into a formatted number using standard North American formatting (nnn) nnn-nnnn
I also will accept Phone numbers with an extension and convert them to (nnn) nnn-nnnn xnnn
By North American phone number (input) I mean a number with 10 digits with the first digit NOT being a 1 or 11 digits with the first digit being a 1
Adapt the code to logic_hook syntax and use this in a before_save logic hook for saving phone numbers with a standard format or in an after_retrieve or process_record to display phone numbers stored as all digits with no formatting.
It strips existing brackets, dashes, periods, hyphens and + signs from existing numbers and replaces with the above standard.
Adjust as desired for your situation. You can add to the pattern and replacement arrays to suit your country standard.
<?php
print_r("Made it to the Phone script".PHP_EOL,false);
$rawPhoneNumber = readline("Enter a number: ");
print_r("The number you entered was: ".$rawPhoneNumber.PHP_EOL);
$onlyAllowedChars = preg_replace("/[^0-9A-Za-z]/", "" , $rawPhoneNumber);
print_r("The number converted to only allowed characters is: ".$onlyAllowedChars.PHP_EOL);
// The following takes:
// a 10-digit number NOT starting with 1 or
// an 11-digit number starting with with 1
// and converts it to standard North American display: (NNN) XXX-XXXX
// If neither of the above two conditions match then the following tests for
// alpha characters following a 10/11 digit number and
// trailing numeric digits following the alpha characters
// and asusmes that an Extension number is included
// so converts to standard North American display
// followed by x followed by the trailing numeric digits
// If none of the above conditions match
// The original alphanumeric entry is shown
$pregPattern = array(
0 => '/^\+?1?(\d{3})(\d{3})(\d{4})$/',
1 => '/^\+?1?(\d{3})(\d{3})(\d{4})([a-zA-Z]{1,})(\d{1,})$/',
);
// ksort not really required in this case since keys are defined to match
// but it is a good habit to get into to always ksort
// the pattern and replacement arrays
ksort($pregPattern);
$pregReplacement = array(
0 => '(\1) \2-\3',
1 => '(\1) \2-\3 x\5',
);
// ksort not really required in this case since keys are defined to match/
// but it is a good habit to get into to always ksort
// the pattern and replacement arrays
ksort($pregReplacement);
// When both pattern and replacement are arrays, matching rules will operate sequentially.
// That is, the second pattern/replacement pair will operate
// on the string that results from the first pattern/replacement pair,
// not the original string.
// This is not a problem for us since with our setup
// the full string (start through to end) is tested
// if the first condition matches, the second will not
// if the first condition does not match, the string returned is the original string
// so the tests will be performed on the original string for each condition
$finalNumber = preg_replace($pregPattern, $pregReplacement, $onlyAllowedChars);
if ( $finalNumber == $onlyAllowedChars ) {
$finalNumber = $rawPhoneNumber;
}
print_r("The final number is: ".$finalNumber.PHP_EOL);
/*
* The above is a fully commented version of the code
* If you want a minimalist version, use this following
*
$rawPhoneNumber = readline("Enter a number: ");
$onlyAllowedChars = preg_replace("/[^0-9A-Za-z]/", "" , $rawPhoneNumber);
$pregPattern = array(
0 => '/^\+?1?(\d{3})(\d{3})(\d{4})$/',
1 => '/^\+?1?(\d{3})(\d{3})(\d{4})([a-zA-Z]{1,})(\d{1,})$/',
);
ksort($pregPattern);
$pregReplacement = array(
0 => '(\1) \2-\3',
1 => '(\1) \2-\3 x\5',
);
ksort($pregReplacement);
$finalNumber = preg_replace($pregPattern, $pregReplacement, $onlyAllowedChars);
if ( $finalNumber == $onlyAllowedChars ) {
$finalNumber = $rawPhoneNumber;
}
print_r("The final number is: ".$finalNumber.PHP_EOL);
*
*/