Login / Signup
|
Forums
User Guide
Chat Room
Packages
UI
Download
Home
»
Packages
» Phone Number Format
Phone Number Format
Version
.01
added 1 month ago by
jackson
{"meta":{"version":"181","build":"1265231004"},"helpers":[{"name":"phone_format","helper_type":"display","phpcode":"<?php\n$phone = $value;\n$convert = false;\n$trim = true;\n\n\t\n\t\/\/ Strip out any extra characters that we do not need only keep letters and numbers\n\t$phone = preg_replace(\"\/[^0-9A-Za-z]\/\", \"\", $phone);\n\t\n\t\/\/ Do we want to convert phone numbers with letters to their number equivalent?\n\t\/\/ Samples are: 1-800-TERMINIX, 1-800-FLOWERS, 1-800-Petmeds\n\tif ($convert == true) {\n\t\t$replace = array('2'=>array('a','b','c'),\n\t\t\t\t '3'=>array('d','e','f'),\n\t\t\t '4'=>array('g','h','i'),\n\t\t\t\t '5'=>array('j','k','l'),\n '6'=>array('m','n','o'),\n\t\t\t\t '7'=>array('p','q','r','s'),\n\t\t\t\t '8'=>array('t','u','v'),\t\t\t\t\t\t\t\t '9'=>array('w','x','y','z'));\n\t\t\n\t\t\/\/ Replace each letter with a number\n\t\t\/\/ Notice this is case insensitive with the str_ireplace instead of str_replace \n\t\tforeach($replace as $digit=>$letters) {\n\t\t\t$phone = str_ireplace($letters, $digit, $phone);\n\t\t}\n\t}\n\n\t\n\t\/\/ If we have a number longer than 11 digits cut the string down to only 11\n\t\/\/ This is also only ran if we want to limit only to 11 characters\n\tif ($trim == true && strlen($phone)>11) {\n\t\t$phone = substr($phone, 0, 11);\n\t}\t\t\t\t\t\t \n\t\n\t\/\/ Perform phone number formatting here\n\tif (strlen($phone) == 7) {\n\t\t$phone = preg_replace(\"\/([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})\/\", \"$1-$2\", $phone);\n\t} elseif (strlen($phone) == 10) {\n\t\t$phone = preg_replace(\"\/([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})\/\", \"($1) $2-$3\", $phone);\n\t} elseif (strlen($phone) == 11) {\n\t\t$phone = 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);\n\t}\n\t\n\t\/\/ Return original phone if not 7, 10 or 11 digits long\n\techo $phone;\n?>"}]}
.01 Notes
Nothing, this be it!
Package Details
All Versions
.01