Home >>Advance PHP Tutorial >PHP Regular Expressions
The Regular expressions are basically the powerful pattern matching algorithm which can be performed in a single expression.
Arithmetic operators such as (+,-, ^) are used by the regular expressions in order to create complex expressions.
Regular expressions are known to accomplish tasks such as validating email addresses, IP address etc.
PHP has its built-in functions allowing the users to work with regular functions. Let's have a look at the most commonly used
regular expressions functions in the PHP
language.
PHP Preg_match to test if the input conforms to the pattern defined for it. The preg_match() function requires two compulsory arguments:
It returns true if a match is found, and false otherwise.
These are the following Constraints
Example
<?php extract($_REQUEST); if(isset($save)) { if(preg_match("/^[a-zA-Z ]*$/",$username)) { echo "Congrates name is ok "; } else { echo "<h3 style='color:Red'>Username not valid</h3>"; } } ?> <html> <head> <title>Regular Express example 1</title> </head> <body> <form method="post"> <table> <tr> <td>Enter Your Name</td> <td><input type="text" name="username"/></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="save" value="check"/></td> </tr> </table> </form> </body> </html>
A common task when working with form is involve checking email adress validation.
Here is the exampe
<?php extract($_REQUEST); if(isset($save)) { if (filter_var($emailid, FILTER_VALIDATE_EMAIL)) { echo "Congrates email is valid"; } else { echo "<h3 style='color:Red'>Invalid email address</h3>"; } } ?> <html> <head> <title>Regular Express Email validation </title> </head> <body> <form method="post"> <table> <tr> <td>Enter Your email</td> <td><input type="text" name="emailid"/></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="save" value="check"/></td> </tr> </table> </form> </body> </html>
Here is the exampe
<?php extract($_REQUEST); if(isset($save)) { if(preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website_url)) { echo "Congrates ! Web Address is Valid"; } else { echo "<h3 style='color:Red'>Invalid Web address</h3>"; } } ?> <html> <head> <title>Regular Express Check URL</title> </head> <body> <form method="post"> <table> <tr> <td>Enter Your Websit URL</td> <td><input type="text" name="website_url"/></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="save" value="check"/></td> </tr> </table> </form> </body> </html>
Metacharacters help us to perform more complex matches pattern in the program
Metacharacters | What it Means | |
---|---|---|
^ | Beginning of String | |
$ | End of String | |
. | Any Character excepa newline character | |
\s | A single whitespace character | |
\S | A single non-whitespace character | |
\d | A digit between 0 and 9 | |
\w | An alphabetic or numeric character, or underscore | |
[A-Z] | An uppercase alphabetic character | |
[a-z] | A lowercase alphabetic character | |
[0-9] | A digit between 0 and 9 | |
| | Or Logical Oprator | |
(?= | Positive conditional test | |
(?! | Negative conditional test |