PHP and regular expression help

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
flamesburn
Forum Newbie
Posts: 3
Joined: Thu Aug 05, 2004 12:39 pm

PHP and regular expression help

Post by flamesburn »

Hello,
Im extremely new to the world of regular expressions and need to create one for a 10 digit phone number that can have spaces dashes and brackets and a postal code that can have 6 characters and be seperated with paces or dashes. does anyone have a good site to read up on regular expressions or kind of point me in the right direction>

thanks in advance

JH
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Here are a few tutorials that I found, but haven't tried, so can't guarentee quality.

http://zend.com/zend/tut/tutorial-delin2.php
http://weblogtoolscollection.com/regex/regex.php
http://www.phpbuilder.com/columns/dario19990616.php3
http://www.phpfreaks.com/tutorials/52/0.php

I've been real pleased with some of the tutorials that i've found at zend.com so i'd probably try the first tutorial first :)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

This program http://www.weitz.de/regex-coach/#install is very useful to test out expressions.
flamesburn
Forum Newbie
Posts: 3
Joined: Thu Aug 05, 2004 12:39 pm

thanks

Post by flamesburn »

thanks for the help, the articles really help and I can definately see how that program comes in helpful. Im just really new at regular expressions.

im trying to validate a postal code i.e. N2R5A9

but I want to be able to have optional spaces and dashes for example

N2R5A9
N2R-5A9
N2R 5A9

should all validate correctly.

the pattern i have come up with does not allow for optional spaces and dashes .......
$pattern ="^[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9]";

any thoughts? im sure its easy once i get a grasp at how to use optional characters.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$pattern = "^\w[ -]?\d[ -]?\w[ -]?\d[ -]?\w[ -]?\d$"
it's for PCRE (preg_*) regexp functions
flamesburn
Forum Newbie
Posts: 3
Joined: Thu Aug 05, 2004 12:39 pm

hmmm

Post by flamesburn »

im using ereg

is there anyway to stay along the lines of the original formatting
$pattern ="^[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9]";

just add in a couple things?

thanks for the help
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

*pssst* Weirdan, that pattern doesn't have starting and ending delimiters for PCRE.. ;) unless I'm thinking of something else.. :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

feyd wrote:*pssst* Weirdan, that pattern doesn't have starting and ending delimiters for PCRE.. ;) unless I'm thinking of something else.. :)
Yup.
Too much JS-ness last days.... Must have been tired....
Post Reply