Posted By

DanielJay on 09/13/07


Tagged

phonereplace


Versions (?)

Who likes this?

18 people have marked this snippet as a favorite

hudge
Abe
wbowers
unravelme1
section31
jdstraughan
Jdub7
hamiltonmascioli
Hilyin
rezzz
nb109
RagnarR
vehler
bobbym245
satsuma
mecha
neex123
ringo380


Format phone


 / Published in: PHP
 

URL: http://www.wlscripting.com/tutorial/49

This script formats USA based phone numbers and has the option to convert phone numbers with letters into numbers.

  1. function formatPhone($phone = '', $convert = false, $trim = true)
  2. {
  3. // If we have not entered a phone number just return empty
  4. if (empty($phone)) {
  5. return '';
  6. }
  7.  
  8. // Strip out any extra characters that we do not need only keep letters and numbers
  9. $phone = preg_replace("/[^0-9A-Za-z]/", "", $phone);
  10.  
  11. // Do we want to convert phone numbers with letters to their number equivalent?
  12. // Samples are: 1-800-TERMINIX, 1-800-FLOWERS, 1-800-Petmeds
  13. if ($convert == true) {
  14. $replace = array('2'=>array('a','b','c'),
  15. '3'=>array('d','e','f'),
  16. '4'=>array('g','h','i'),
  17. '5'=>array('j','k','l'),
  18. '6'=>array('m','n','o'),
  19. '7'=>array('p','q','r','s'),
  20. '8'=>array('t','u','v'), '9'=>array('w','x','y','z'));
  21.  
  22. // Replace each letter with a number
  23. // Notice this is case insensitive with the str_ireplace instead of str_replace
  24. foreach($replace as $digit=>$letters) {
  25. $phone = str_ireplace($letters, $digit, $phone);
  26. }
  27. }
  28.  
  29. // If we have a number longer than 11 digits cut the string down to only 11
  30. // This is also only ran if we want to limit only to 11 characters
  31. if ($trim == true && strlen($phone)>11) {
  32. $phone = substr($phone, 0, 11);
  33. }
  34.  
  35. // Perform phone number formatting here
  36. if (strlen($phone) == 7) {
  37. return preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "$1-$2", $phone);
  38. } elseif (strlen($phone) == 10) {
  39. return preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "($1) $2-$3", $phone);
  40. } elseif (strlen($phone) == 11) {
  41. return preg_replace("/([0-9a-zA-Z]{1})([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "$1($2) $3-$4", $phone);
  42. }
  43.  
  44. // Return original phone if not 7, 10 or 11 digits long
  45. return $phone;
  46. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Mackski on August 31, 2009

Added support for mobile or cell phone detection (Australia)

 function formatPhone($phone = '', $cell=false, $convert = false, $trim = true)

And:

 if (strlen($phone) == 7) {
            return preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "$1-$2", $phone);
} elseif (strlen($phone) == 10 && !$cell) {
    return preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "($1) $2-$3", $phone);
} elseif (strlen($phone) == 10 && $cell) {
    return preg_replace("/([0-9a-zA-Z]{4})([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})/", "$1 $2 $3", $phone);
} elseif (strlen($phone) == 11) {
    return preg_replace("/([0-9a-zA-Z]{1})([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "$1($2) $3-$4", $phone);
}
Posted By: chrsc on December 8, 2009

Great script, I have an addition for php 4 users:

// Strip out any extra characters that we do not need only keep letters and numbers
$phone = preg_replace("/[^0-9A-Za-z]/", "", $phone);

// check for php version 4
if(substr(phpversion(), 0, 1) == '4') $phone = strtolower($phone);
    foreach($replace as $digit=>$letters) {
        if(substr(phpversion(), 0, 1) == '5') $phone = str_ireplace($letters, $digit, $phone);
        else $phone = str_replace($letters, $digit, $phone);
    }
Posted By: chrsc on December 8, 2009

Opps, sorry that comment didn't get all my updates:

  1. From line 8, add check for php 4, and replace with lowercase string // Strip out any extra characters that we do not need only keep letters and numbers $phone = preg_replace("/[^0-9A-Za-z]/", "", $phone);

    // check for php version 4 if(substr(phpversion(), 0, 1) == '4') $phone = strtolower($phone);

  2. Line 24, again check for php 4 and use strreplace instead of strireplace foreach($replace as $digit=>$letters) { if(substr(phpversion(), 0, 1) == '5') $phone = strireplace($letters, $digit, $phone); else $phone = strreplace($letters, $digit, $phone); }

You need to login to post a comment.