note 84560 added to function.ereg

From: Date: Fri, 18 Jul 2008 20:16:48 +0000
Subject: note 84560 added to function.ereg
Groups: php.notes 
Request: Send a blank email to php-notes+get-141668@lists.php.net to get a copy of this message
In reply to Francis, Your regular expression: $pattern = "^(\()?([0-9]{3})(\))?( )?([0-9]{3})( )?(\-)?( )?([0-9]{4})$"; I actually used this to validate a phone number. The one discrepancy I found is with patterns such as 503-555-1212, 503-5551212, [503]5551212, and every other pattern that does not have single whitespace or a dash as a delimiter, ereg() will return false when given your expression. Based on your pattern, I came up with a more general and concise version making use of the dot meta-character: $pattern = "^.?[0-9]{3}.?.?[0-9]{3}.?[0-9]{4}$"; When passed to ereg(), it will return "true" so long as there is 10 digits, regardless of the delimiter chosen by the user. [503] 5551212 //true 5035551212 //true 503+555 1212//true $pattern = "^(\()?([0-9]{3})(\))?( )?([0-9]{3})( )?(\-)?( )?([0-9]{4})$"; ---- Server IP: 209.41.74.194 Probable Submitter: 75.144.146.97 ---- Manual Page -- http://www.php.net/manual/en/function.ereg.php Edit -- https://master.php.net/note/edit/84560 Del: integrated -- https://master.php.net/note/delete/84560/integrated Del: useless -- https://master.php.net/note/delete/84560/useless Del: bad code -- https://master.php.net/note/delete/84560/bad+code Del: spam -- https://master.php.net/note/delete/84560/spam Del: non-english -- https://master.php.net/note/delete/84560/non-english Del: in docs -- https://master.php.net/note/delete/84560/in+docs Del: other reasons-- https://master.php.net/note/delete/84560 Reject -- https://master.php.net/note/reject/84560 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141668) next »