Sujit Kumar Shah
Freelance PHP MySql programmer
Regular Expression Rule & Example
The regular expression, as a pattern, can match all kinds of text strings helping your application validate, compare, compute, decide etc. It can do simple or very complex string manipulations. The list of possibilities is enormous when it comes to what you can achieve using regular expressions. You can take any phrase that starts with an "A" or any A - Z character and do various things with it. You can match phone numbers, email addresses, URL, credit card numbers, social security numbers, zip codes, states, cities.....(the list never ends). A huge script that is supposed to validate a user input and prepare it for SQL can be reduced to only one line with the help of preg_replace.
There are two types of regular expression functions in PHP:
- ereg functions or POSIX extended regular expression function, which is the standard functions for PHP (i.e. ereg, ereg_replace, eregi, eregi_replace)
- preg functions or perl Compatible regular expressions (i.e. preg_grep, preg_match, preg_match_all(), preg_replace, preg_replace, preg_replace_callback(), preg_split).
Some basic Regular Expression Rules are as below:
^ Start of String
$ End of string
n* Zero or more of 'n'
n+ One or more of 'n'
n? A possible 'n'
n{2} Exactly two of 'n'
n{2,} At least 2 or more of 'n'
n{2,4} From 2 to 4 of 'n'
() Parenthesis to group substrings
(n|a) Either 'n' or 'a'
. Any single character
[1-7] A number between 1 and 7
[b-t] A lower case character between b and t
[B-T] An upper case character between B and T
[^a-z] Absence of lower case a to z
[_a-zA-Z] An underscore or any letter of the alpha
\w Any upper or lower case letter or number or underscore
\W Any character not in \w
\d Any digit 0-9
\D Any character not in \d
i A modifier - goes at the end of the pattern string - ignore case
Here is the PHP source code for email validation using PHP regx function:
<?php
$email = "myemail@mydomain.com";
if (ereg('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.([a-zA-Z]{2,4})$',$email)
{ echo 'Valid email id';}
else
{echo 'Invalid email id';}
?>
Now I am going to split the string pattern into 3 parts, ie
^[a-zA-Z0-9._-]+@Here ^ symbol denotes that this is the start of the email part.
[a-zA-Z0-9._-]+.
([a-zA-Z]{2,4})$
a-zA-Z0-9._- denotes combination of characters to form the username section of an email
+ symbol denotes, it should have 1 or more characters from the proceeding sets
@ symbol is a default symbol between username and domain name parts.
a-zA-Z0-9._- denotes combination of characters to form the domain name without the tld
+ symbol denotes, it should have 1 or more characters from the proceeding sets
. symbol denotes, the dot operator proceeding to the tld, here we are using to escape
a-zA-Z denotes the combination of chars to form the tld name
{2,4} denotes the length of the tld may be between 2 to 4 characters
$ denotes the end of an email.
Tags: Regex, Basic, Beginner, Validation, Rule
To do articles more effectively, we need your comments or suggestions on how to improve the articles and make it more useful, or what other articles you would like in the future. Please CLICK HERE and leave your feedback. Thanks!!!
- Ajax autocomplete using PHP & MySQL
- XML Web Service using PHP and SOAP
- Simple JQuery Accordion menu
- Reduce High CPU usage overload problem caused by MySql
- Pure CSS Bubble Tooltips
- How to make your website load fast?
- Pure JavaScript Search and Text Highlighting
- Getting real IP address in PHP
- Generating CAPTCHA Image Using PHP
- Basic security vulnerabilities in php code
Categories
Archives
Website Services
- » Web 2.0 Website Design
- » Online Shopping (E-commerce)
- » Content Management System
- » Small/Advanced Business Sites
- » Search Engine Optimization
- » Website Maintenance Services
- » Web and Enterprise Portal Development
- » Directory Website
- » Auction, Bidding Website
- » Classified, Marketplace Website
- » Web Application Development
