Solved

Syntax used with ereg

Posted on 2004-04-14
2
353 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 …

773 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