Understanding PHP Regular Expression

PHP is mostly used scripting language in web world. Regular expression of PHP is one of the most important key to handle the form validation. This tutorial, I am going to discuss about the PHP regular expression.

 

PHP Regular Expression tisuchi.wordpress.com

 

 

 

 

 

Fundamentally, there are few symbols to identify the users input. The list are follows with the explanation.

Symbol             Explanation

^                       Start of string
                      End of string
.                        Any single character
+                       One or more character
\                        Escape Special characters
?                       Zero or more characters

 

Look exact matching of code with “abc”.

$express = /^abc$/;

Look for starting letters with “abc….”.

$express = /^abc/;

Check letters end with “abc”.

$express = /abc$/;

Check the abc input with one single characters “abcs”

$express = /^abc.$/;

Check the abc text with more then one characters like abcsy

$express = /^abc.+$/;

Passed any characters like abcstuvw123…

$express = /^abc.+?$/;

Now, going to advanced option for regular expression.

Char                Group Explanation

[abc]                 Should match any single of character
[^abc]              Should not match any single character
[a-zA-Z0-9]   Characters range lowercase a-z, uppercase A-Z and numbers
[a-z-._]            Match against character range lowercase a-z and ._- special chats
(.*?)                  Capture everything enclosed with brackets
(com|info)     Input should be “com” or “info”
{2}                     Exactly two characters
{2,3}                 Minimum 2 characters and Maximum 3 characters
{2,}                   More than 2 characters

 

Checking the different kinds of URL by using the PHP regular expression.

$check_url = /^(http|https|ftp):\/\/(www+\.)? [a-zA-Z0-9]+\.([a-zA-Z]{2,3})\/?/;

This regular expression will cover the following URI’s.

 

Check the digits by using PHP regular expression.

Short Form     Equivalent              Explanation 

\d                      [0-9]                         Any numbers
\D                     [^0-9]                      Any non-digits
\w                     [a-zA-Z0-9_]       Characters,numbers and underscore
\W                    [^a-zA-Z0-9_]    Except any characters, numbers and underscore
\s                       –                                White space character
\S                      –                                Non white space character

Hope, this tutorial will make clear your concept about PHP regular expression. Best of luck.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s