|
Posted by David Haynes on 07/05/06 15:36
Bob Bedford wrote:
> Hello there,
>
> I need help: I've a string that is used to write down a phone number that
> comes from a form and would like to keep it as clean as possible. For
> avoiding people to create twice an account using a different syntax.
>
> For this the only char I'd like to allow are number 0-9, '/','.' and spaces.
>
> I'd like to replace everything other by a "space".
preg not ereg but it can easily do what you want.
I got this from the PHP manual comments (I think)
/**
* Validates and reformats (if necessary) a North American telephone
* number. Telephone number is reformatted to the following format:
* 123-123-1234 x12345
*
* @param string $phone
* @return string or FALSE
*/
function checkTelephone($phone) {
$na_fmt ="/^(?:\+?1[\-\s]?)?(\(\d{3}\)|\d{3})[\-\s\.]?"; //area
code
$na_fmt.="(\d{3})[\-\.]?(\d{4})"; // seven digits
$na_fmt.="(?:\s?x|\s|\s?ext(?:\.|\s)?)?(\d*)?$/"; // any extension
if( ! preg_match($na_fmt, $phone ,$match) ) {
return false;
} else {
$ret = '';
if( substr($match[1], 0, 1 ) == '(') {
$ret .= $match[1];
} else {
$ret .= $match[1].'-';
}
$ret .= $match[2].'-'.$match[3];
if ($match[4] != '') {
$ret.=' x'.$match[4];
}
return $ret;
}
}
-david-
[Back to original message]
|