naz on 03/03/09

# Statistics

Viewed 716 times
Favorited by 9 user(s)

# Phone number beutifier

/ Published in: PHP
`function format_phone(\$phone)    {    	\$prefix = null;     	//Check if number has international prefix    	switch(true){    		case substr(\$phone, 0, 1) == '+':    			\$phone = substr(\$phone, 1);    			\$prefix = '+';    		break;    		case substr(\$phone, 0, 2) == '00':    			\$phone = substr(\$phone, 2);    			\$prefix = '+';   			break;    	}     	//Strip all non numeric characters        \$phone = preg_replace("/[^0-9]/", '', \$phone); 		switch(strlen(\$phone)){			case 6:				return preg_replace("/([0-9]{2})([0-9]{2})([0-9]{2})/", "\$1-\$2-\$3", \$phone);                        break;			case 7:				return preg_replace("/([0-9]{3})([0-9]{4})/", "\$1-\$2", \$phone);			break;			case 10:				return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "(\$1) \$2-\$3", \$phone);			break;			case 11:				return preg_replace("/([0-9]{3})([0-9]{4})([0-9]{4})/", \$prefix . " (\$1) \$2-\$3", \$phone);			break;			case 12:				return preg_replace("/([0-9]{4})([0-9]{4})([0-9]{4})/", \$prefix . " (\$1) \$2-\$3", \$phone);			break;						default:				return \$phone;			break;		}    }`