Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- Preg Match Problems (I Think!)


rocknbil - 4:21 pm on Mar 24, 2011 (gmt 0)


Expanding the pre-dash/postdash to limits, say, a "between 4 and 6 before the dash and 8 and 12 after the dash"

preg_match("/^\d{4,6}-\d{8,12}$/", $_POST['name']);

Or is the dash optional? * means zero or more, ? means zero or one.

preg_match("/^\d{4,6}-?\d{8,12}$/", $_POST['name']);

Which is really kinda funky, because it means "4-6 which can be followed by 8-12" which really means "4-18." :-)

4 or unlimited:

preg_match("/^\d{4,}$/", $_POST['name']);

A usability thing, you might just "fix" it for them before your test so you don't have to throw an error. Remove anything but \d or dash.

$_POST['name'] = preg_replace('/[^\d\-]/','',$_POST['name']);
preg_match("/^\d{4,6}-\d{8,12}$/", $_POST['name']);

... will insure what you're testing is just numbers and dashes, then test against the format.


Thread source:: http://www.webmasterworld.com/php/4286350.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com