PHPBuilder - phonetest



RSS Twitter
Snippets Algorithms

phonetest

by: Matt Larson
|
December 6, 2003

Version: 0.1

Type: Function

Category: Algorithms

License: GNU General Public License

Description: takes user input, parses out any non-numeric characters, and returns the phone number in the form (###) ###-#### x#### (if more than 10 digits).



<?php
	function phonetest($phone) {
	
	// tests for 10 or more digits and fomats in the form (###) ####-#### x#####
	// returns no value if phone number is not valid
	
		$len=strlen($phone);
		$phonenums="";
		for ($x=0;$x<$len;$x++){
			$c=substr($phone,$x,1);
			if (ereg("([0-9])",$c)){
				$phonenums=$phonenums.$c;
				}
			}
				
		if(strlen($phonenums)<10){
			return("");
			}
				
		$area=substr($phonenums,0,3);
		$exchange=substr($phonenums,3,3);
		$no=substr($phonenums,6,4);
			
		$newphone="(".$area.") ".$exchange."-".$no;
			
		if(strlen($phonenums)>10){
			$rest=strlen($phonenums)-10;
			$extention=substr($phonenums,10,$rest);
			$newphone=$newphone." x".$extention;
			}
			
			return($newphone);
		}
?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Matt Larson

Comment:



Comment:

(Maximum characters: 1200). You have characters left.