IDIOT DEVELOPER

Regular Expression in PHP
Regular Expression in PHP

In this tutorial we are going to learn Regular Expression in PHP. Regular Expression is a powerful pattern matching algorithm. It is used to for matching or identifying pattern in a string. These are generally used for validating data entered by the user via a form. The data can be like an email – checking whether the email entered by the user has correct syntax.

 

We are going to use three PHP functions.

 

1. preg_match()

2. preg_split()

3. preg_replace()

 

But before discussing these three methods, we need to discuss about the Meta Characters used in regular expression.

 

Meta Character

There are two types of meta characters:

 

1. The meta character that are recognized everywhere except square bracket.

2. The meta character that are recognized within the square brackets.

 

Meta Characters outside square bracket

 

Meta-character Description
. match any character except newline
^ assert start of string
$ assert end of string
\ used as escape character
[…] character class
| start of alternative branch
(…) subpattern or subexpression
?  also 0 or 1 quantifier
* 0 or more quantifier
+ 1 or more quantifier
{ start min/max quantifier
} end min/max quantifier

 

Meta Characters inside square brackets

The pattern that is within the square brackets are called character class.

 
Meta-character Description
\ general escape character
^ negate the class, but only if the first character
indicates character range

 

Character Class Shortcut

 
Meta-character Description
\d matches any number, equivalent to [0-9]
\D matches every character that is not a number, equivalent to [^0-9] (the ^ inside square brackets has a negation meaning)
\w every word character, equivalent to [A-Za-z0-9_]. Letters, numbers and the underscore.
\W every non word character, equivalent to [^A-Za-z0-9_] (examples of non word could be a newline \n, a tab \t or a comma ,)
\s matches spaces
\I lowercase letters, equivalent to [a-z]

 

preg_match()

This PHP function is used to perform a pattern match on a string. It takes two parameters.

 

1. The pattern.

2.The string.

 

If the correct pattern is found it return 1, or else it return 0.

 


$url = "http://idiotdeveloper.tk";
$r = preg_match("/developer/", $url);

 

preg_split()

This PHP function is used to perform a pattern match on a string and then split the string into a numeric array.  If it cannot find the pattern in the string , it will return an array with that string at index 0.

 

For example, we have a string.

 


$str = "this-is-a-url";

 

Now we will perform preg_split function on it. This function takes two parameter.

1. The pattern.

2. The string.

 


$r = preg_split("/-/", $str);

echo '<pre>';
print_r($r);
echo '</pre>';

 

It will return a numeric array.

 


Array
(
    [0] => this
    [1] => is
    [2] => a
    [3] => url
)

 

preg_replace()

This PHP function is used to perform a pattern match on a string. It takes three parameters.

 

1. The pattern.

2. The word to replace.

3. The string.

 

If the function finds the pattern in the string, it replaces it and return the new string, otherwise it will return the old(given) string.

 


$str = "hello world";
$r = preg_replace("/world/", "universe", $str);
echo $r;

One comment on “Regular Expression in PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *