PHP preg_match question

Matt Graham danceswithcrows at usa.net
Wed Jul 20 21:41:30 MST 2011


From: keith smith <klsmith2020 at yahoo.com>
> I have an input string that should only be lower or upper alphas,
> numbers and can contain a hyphen.  I'm trying to figure out how
> to get PHP preg_match to verify the input string only contains these
> chars.

> preg_match("/[a-z0-9\+\-]/", $vaidateStr) 

<?php
if(preg_match("/^[a-zA-Z0-9\-]+$/","stuff23AA")){
   print "This should print\n";
   }
if(preg_match("/^[a-zA-Z0-9\-]+$/","znork_")){
   print "This should not print.\n";
   }

...The regexp there means "beginning of string, then a sequence of one or more
characters from a-z A-Z 0-9 and -, then the end of the string".  So
"stuff23AA" matches, while "znork_" doesn't.  Regular expressions can be a
little funky, which is why you always test them against a bunch of stuff
before using them....

-- 
Matt G / Dances With Crows
The Crow202 Blog:  http://crow202.org/wordpress/
There is no Darkness in Eternity/But only Light too dim for us to see



More information about the PLUG-discuss mailing list