What is Regex?
Regular expressions (regex) are a sequence of specific characters itself. Generally the regex consists of arithmatic operators & alphanumeric characters to construct specific patterns.
These patterns are widely used for customized validation as per user requirement.
Functions
PHP provides us three functions to deal with regular expressions:
preg_match: The function is used to check pattern match on a string. It returns true when
preg_split: The function is used for pattern matching on a string and then splits the results into a numeric array
preg_replace: The function is used to identify a pattern match on a string and replace the matched portion with the specified text.
Below is the general syntax for a regex function such as preg_match, preg_split or preg_replace.
Syntax–
1 | <php func_Ebhor('/Ebhor_regex/',Ebhor_txt); ?> |
Here,
“func_Ebhor” is one of the three functions already described above.
The forward slashes denote the starting and ending of the specific regular expression
“Ebhor_regex” is the particular pattern that needs to be matched
“Ebhor_txt” is the text string needs to be matched with Ebhor_regex.
Meta characters
The meta characters help us to perform complex pattern matches such as test the validation of an email, telephone, unique username etc. Here is some commonly used meta characters:
Meta Character | Description/Example |
. | Dot matches any single character except a new line character././ matches anything having single character in it |
^ | It matches the beginning of a string./^eb/ matches any string that begins with eb |
$ | Dollar sign matches pattern the end of the string/in$/ matches robin, coffin etc. |
* | Star matches zero (0) or more characters in the given string./con*/ matches continuous, control, connection etc. |
+ | Plus requires preceding character(s) to appear at least once./ebh+or/ matches ebhor |
\ | Backslash is helpful to escape meta characters./ebhor+\.com/ considers the dot as a literal value during pattern match |
[…] | Character class/[abc]/ matches abc |
a-z | It matches lower case letters./a-z/ matches cool, happy etc. |
A-Z | It matches upper case letters like EBHOR, PHP, REGEX etc. |
0-9 | It simply matches any number between 0 and 9./0-3/ matches 0,1,2,3 etc. |
Predefined Character Ranges
There are some predefined character ranges, known as character classes, are available in PHP for easier coding.
Character classes contain a collection of specific characters within a range. Some of the most popular character classes are given below:
Sr.No | Expression & Description |
1 | [[ |
2 | [[ |
3 | [[: |
4 | [[ |
Examples
Here is an example of PHP validation using regular expressions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php $name_Error = $email_Error = $url_Error = $mobile_Error = $user_name = $user_email = $user_url = $user_mobile = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $user_name = test_input($_POST["name"]); if (!preg_match(‘/^[a-zA-Z ]*$/’,$user_name)) { // regex for valid name $nameError = "Only letters and white spaces are allowed!"; } $user_email = test_input($_POST["email"]); if (!preg_match(‘/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z.]{2,4}$/ ‘, $user_email)) { // regex for valid email $email_Error = "Invalid email!"; } $user_url = test_input($_POST["url"]); if (!preg_match(‘/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i’,$user_url)) { // regex for valid URL $url_Error = "Invalid URL!"; } $user_mobile = test_input($_POST["mobile"]); if (!preg_match('/^[0-9]{10}+$/', $user_mobile)){ // regex for 10 digit mobile number $mobile_Error = "Invalid mobile number!"; } ?> |