note 101326 deleted from function.preg-replace by danbrown
| From: | danbrown@php.net | Date: | Fri, 10 Dec 2010 17:12:00 +0000 |
| Subject: | note 101326 deleted from function.preg-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174778@lists.php.net to get a copy of this message | ||
Note Submitter: Meisam Mulla
----
I needed a function that validated phone numbers provided by a user and if necessary arrange the
numbers in this format 555-555-5555. I couldn't find anything so I just wrote one
<?php
function checkPhone($number)
{
if(preg_match('^[0-9]{3}+-[0-9]{3}+-[0-9]{4}^', $number)){
return $number;
} else {
$items = Array('/\ /', '/\+/', '/\-/', '/\./',
'/\,/', '/\(/', '/\)/', '/[a-zA-Z]/');
$clean = preg_replace($items, '', $number);
return substr($clean, 0, 3).'-'.substr($clean, 3, 3).'-'.substr($clean, 6, 4);
}
}
?>