Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Syntax used with ereg

Posted on 2004-04-14
2
Medium Priority
?
359 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 800 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 200 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

Independent Software Vendors: 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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

609 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