Solved

Syntax used with ereg

Posted on 2004-04-14
2
351 Views
Last Modified: 2006-11-17
Could someone explain the following code in English please:

$validFormat = ereg("^(3[0-9]{4}|2131|1800)[0-9]{11}$", $this->__ccNum);

The code is validating a JCB type credit card number. I've read an explanation for the function ereg at http://www.phpbuilder.com/manual2/manual/fi/function.ereg.php , but I'm looking for a bit more help.

eg how does this work: "^(3[0-9]{4}|2131|1800)[0-9]{11}$"
what does ^ mean?
I think | = or

The valid format for JCB is:
1. prefix = 3 and lenght = 16 or
2. prefix = 2131 or 1800 and length = 15
0
Comment
Question by:ncw
2 Comments
 
LVL 8

Accepted Solution

by:
HaydnH earned 200 total points
ID: 10822404
Check out the regex cheatsheet:

    http://regexlib.com/CheatSheet.htm


^ means "start of string"... yes, "|" is similar to or...

Explained:

^
If we have a "Start of string"...

(3[0-9]{4}|2131|1800)
...followed by 3NNNN OR 2131 OR 1800 where N is any number 0-9...

[0-9]{11}
...followed by 11 numbers betweeon 0 and 9...

$
...followed by an "end of string"

... then it's valid.

Haydn.
0
 
LVL 12

Assisted Solution

by:venkateshwarr
venkateshwarr earned 50 total points
ID: 10822483
if you split it you will have two parts

^(3[0-9]{4}|2131|1800)         and           [0-9]{11}$

In, ^(3[0-9]{4}|2131|1800)   ^ means starts with the following element(s) (here 3[0-9]{4}|2131|1800)

here 3[0-9]{4}|2131|1800 means
first number is 3, followed by any 4 digits (shown in {} brackets) between 0 and 9 (shown in [] brackets)
or
2131
or
1800

'|' symbol means "or"

So on the whole ^(3[0-9]{4}|2131|1800)  means,
the string starts with 3, followed by any 4 digits or
the string starts with 2131 or
the string starts with 1800.

then he next part,  [0-9]{11}$
Here '$' sign indicates "ends with"
[0-9]{11} means any 11 numbers between 0 and 9

check out this link if you need more details...
http://www.regular-expressions.info/reference.html
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now