regular expressions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

regular expressions

Post by dannymc1983 »

does anyone know what the regular expression for the following are:
1) a string that contains only the digits 0-9, can contain only 7 digits (no more, no less) and cannot contain spaces
2) a stirng that contains only characters a-z, both uppercase and lowercase and can contain spaces. ie. it is a stirng used for storing a name
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

preg_match('#^ї0-9]{7}$#', $text1);
preg_match('#^їA-Z ]$#i', $text2);
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

1) a string that contains only the digits 0-9, can contain only 7 digits (no more, no less) and cannot contain spaces

#^[0-9]{7}$#
2) a stirng that contains only characters a-z, both uppercase and lowercase and can contain spaces. ie. it is a stirng used for storing a name
#^[a-zA-Z ]$#

Warning I'm a regex noob. So test these.

Feyd will probably answer this post before I ever hit submit...

Is that right Feyd?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

tooo slow.. :P
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post by dannymc1983 »

hey feyd
thanks for reply
1st reg expression runs ok
2nd reg expression (to identify names, ie upper and lowercase letters only, spaces allowed) doesnt seem to be working. this example has your regular expression, see what happens when you run it:

<?
$name = "Danny";

if (!preg_match('#^[a-zA-Z]$#i', $name))
echo "invalid";
else
echo "valid";

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

oops.. my brain was off..

Code: Select all

#^&#1111;A-Z ]+$#
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post by dannymc1983 »

ok its working now, kind of......
only thing it wont accept is a name with spaces, like the following example:

<?

$name = "mc donald";

if (!preg_match('#^[a-zA-Z]+$#', $name))
echo "invalid";
else
echo "valid";


?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

look at my post again...
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post by dannymc1983 »

:oops:
missed that!
thanks
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post by dannymc1983 »

just one last thing, how would i incorporate a ' into the regular expression
to allow for names like o'donnell.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Just add a backslash floowed by a single quote in the square brackets... dude, all you're doing is specifying the permitted characters ;-)

Code: Select all

#^&#1111;A-Z'' ]+$#i
Post Reply