note 58193 added to function.preg-match

From: Date: Thu, 27 Oct 2005 05:37:39 +0000
Subject: note 58193 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-97652@lists.php.net to get a copy of this message
Test for valid US phone number, and get it back formatted at the same time: function getUSPhone($var) { $US_PHONE_PREG ="/^(?:\+?1[\-\s]?)?(\(\d{3}\)|\d{3})[\-\s\.]?"; //area code $US_PHONE_PREG.="(\d{3})[\-\.]?(\d{4})"; // seven digits $US_PHONE_PREG.="(?:\s?x|\s|\s?ext(?:\.|\s)?)?(\d*)?$/"; // any extension if (!preg_match($US_PHONE_PREG,$var,$match)) { return false; } else { $tmp = "+1 "; if (substr($match[1],0,1) == "(") { $tmp.=$match[1]; } else { $tmp.="(".$match[1].")"; } $tmp.=" ".$match[2]."-".$match[3]; if ($match[4] <> '') $tmp.=" x".$match[4]; return $tmp; } } usage: $phone = $_REQUEST["phone"]; if (!($phone = getUSPhone($phone))) { //error gracefully :) } ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58193 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58193&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58193&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58193&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58193&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58193&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58193&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58193&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58193&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97652) next »