Solved

preg_match in PHP

Posted on 2014-11-20
6
123 Views
Last Modified: 2014-11-23
Any idea what this preg_match want to do ? Tks

if(!preg_match(“/^[4]{1,1}[0-9]{12,15}$/”,$value))
0
Comment
Question by:AXISHK
6 Comments
 
LVL 11

Accepted Solution

by:
Radek Baranowski earned 200 total points
Comment Utility
http://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm

it matches a string that
begins (^) with exactly one ({1,1}) "4" then is followed by 12 to 15 digits 0-9

so, the pattern like:

41234567890123

hope this helps!
0
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 100 total points
Comment Utility
I think the string was done so you can quickly change the amounts of digits at the beginning, or else {1,1} is meaningless.
As is, the pattern can be written as
^4\d{12,15}$

HTH,
Dan
0
 

Author Comment

by:AXISHK
Comment Utility
^4\d{12,15}$

Does it mean starting with '4", following by 12 - 15 digit number ? Tks
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:Radek Baranowski
Comment Utility
yes.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
There is one other problem.  The apparent double quote wrapping the regular expression are not, in fact, quotes.  They may be something that wormed its way in from cut/paste with Microsoft word?  You may want to read this article about how PHP handles quote marks.  It's an important part of the language with many effects on the meaning of the code.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html

You may find that your regular expressions are easier to craft and understand if you write them like this, with comments to explain the intent of each segment of the expression:
$rgx 
= '#'           // REGEX DELIMITER (REQUIRED AT FRONT AND BACK OF EXPRESSION)
. '^'           // AT THE START OF THE STRING
. '[4]'         // CHARACTER CLASS THAT INCLUDES ONLY "4"
. '{1,1}'       // EXACTLY ONE OF THIS CHARACTER CLASS
. '[0-9]'       // CHARACTER CLASS THAT INCLUDES DIGITS 0-9
. '{12,15}'     // BETWEEN 12 AND 15 OF THE DIGITS
. '$'           // AT THE END OF THE STRING
. '#'           // REGEX DELIMITER
;

Open in new window

In the instant case this looks like  the regex is intended to match certain kinds of credit card numbers.
0
 

Author Closing Comment

by:AXISHK
Comment Utility
Tks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
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 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 …

762 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

13 Experts available now in Live!

Get 1:1 Help Now