Solved

Syntax used with ereg

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
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.

730 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