Solved

Syntax used with ereg

Posted on 2004-04-14
2
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

688 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