Regular expressions are used to search specific patterns in strings. PHP supports two styles of regular expressions: POSIX and Perl.
POSIX style of regular expressions is compiled in PHP by default. We can use Perl style also by using Perl compatible regular expression compiling.Regular expressions are basically used in conditions where we have to search pattern that is not exact. If the pattern we have to search is exact we can use string functionsalso.
In regular expressions we can search for a pattern that may occur at starting or ending of string. Part of pattern is repeated etc by using various meta-characters.
A regular expression is a string used to describe, parse or search text within a string.
For example:
If we have to search a person with name “Bill smith” or “William smith”.
If we have to search strings start with letter a.
Check whether the entered email is valid or not.
Check for valid URL, complex patterns
Explode a string on complex patterns.
Characters & Symbols
In regular expressions, we can use some special characters denoting special meaning; these are called literals or metacharacters.
Symbol | Meaning |
/ | Delimiter |
^ | Indicate start of line |
$ | Indicate the end of line |
\ | Indicate general escape character |
{ } | Indicates occurrence of character |
{ n,m} | Indicates minimum and maximum occurrence of character |
( ) or [ ] | Used to enclose a pattern |
| | Used to separate alternative patterns |
* | Indicates single character repeat 0 or more times |
. | Matches any character except newline |
+ | Matches any character repeat 1 or more times |
Character classes in regular expressions
Some character classes can be used in regular expressions:
Class | Description |
\d | Indicates any decimal digit |
\D | Indicates any character that is not a decimal digit |
\s | Indicates any whitespace character |
\S | Indicates any character that is not a whitespace character |
\w | Any word character |
\W | Indicates any non word character |
\b | Indicates word boundary |
\B | Indicates not a word boundary |
\A | Indicates start of subject |
\Z | Indicates end of subject or newline at end |
\z | Indicates end of subject |
\G | Indicates first matching position in subject |
Regular expression functions
Several basic functions can also be used in regular expressions:
ereg( )
The prototype of ereg() function is:
int ereg(string pattern, string search, array[matches]);
This function searches string for a specific pattern, if matches found they will be stored in array matches.
eregi()
This function is same as ereg(), the only difference is that it is not case sensitive.
Replacing substrings with regular expressions
Regular expressions can be used to find and replace substrings also. For that we can use following functions:
ereg_replace()
The prototype of this function is:
String ereg_replace(string pattern, string replacement, string search);
This function is used to search for the regular expression pattern and replace it with the string replacement.
eregi_replace()
This function works same as ereg_replace, but it is not case sensitive
split()
This function takes a pattern, a target string and an optional number of characters to split. It returns an array of strings created by splitting the target string into chunks.
spliti()
Same as split() but is case insensitive.
Preg Regular expression functions
Function | Description |
Preg_grep | It returns array contents that match pattern |
Preg_match_all | It performs global regular expression match |
Preg_match | It performs a regular expression match |
Preg_quote | It is used for quote regular expression character |
Preg_replace | It performs a regular expression search and replace |
Preg_split | It is used for splitting string by regular expression |
Preg_replace_callback | It is same as preg_replace, difference is that instead of a replace string a callback function is used |
For example: if we to check valid email then:
Email addresses are in following format:
v Then we look on user name up to @.
v Start the expression with ^ and then use[ ] to group the possible values, which can be upper and lower case and 0-9 numbers, like:
^ [A-Za-z0-9] + @
Here +@ indicates that there must be something before the @ to be a valid email address.
v Now we have to check domain name up to character “.”, like
[A-Za-z0-9]+\
Here + means there must be something in the first part before “.”
Here “\ .” character indicates that “.” Is escaped using “\”, that escapes the regular meaning of “.”
v Now we have to check domain extension character:
Use [ ] to contain the possible values.
The meaning of {2,4} means there must be at least 2 and maximum 4 characters can be used.
\Z indicates no additional characters will be used as part of this validation.
Example: to validate an URL
<?php function check_url($url) { $result=ereg('^www\\.[a-z]+\\.com$',$url); return $result; } $url_array=array('www.google.com','www.microsoft.com','www.zend.com','www.sun.com','yahoo.com','www.my tutorials.com'); while($test=array_pop($url_array)) { if(check_url($test)) print("\"$test\" is a correct url<br/>"); else print("\"$test\" is an incorrect url<br/>"); }
When we run this script we will get warning message:
Function ereg() is deprecated
It can be removed by disabling the error_reporting E_ALL & ~E_DEPRECATED setting in php.ini file .
The output of above code is:
Exception Handling
When we develop any application, error can be occurred. Errors can be classified as syntactical, logical and runtime errors.
Run time errors are basically known as Exceptions. When exceptions occur in an application during execution time, it can be displayed in browser or application will be terminated abruptly. So to overcome this situation we should handle the exceptions during runtime, so that runtime errors can be handled appropriately.
In PHP exceptions are belong to Exception class or its subclasses. Exceptions can be raised using throw and can be handled using try/catch block. We can extend Exception class also to define our own custom exceptions.
In exception handling, the code where exception can be raised should be written in try block. The code to handle exception should be written in catch block.
Exception is an object that will be thrown and stop processing until it is either caught or left unhandled. In PHP exception can be thrown by using following syntax:
<?php try { throw new Exception(‘Exception message to be displayed’); } ?>
If there is any exception, it can be handled also, for that we should define catch block as follows:
<?php try { Throw new Exception(‘Exception message to be displayed’); } catch(Exception $e) { // The code to handle exception } ?>
Defining custom exceptions
We can inherit Exception class in the following way:
<? class custom Exception extends Exception { function _construct($message) { Parent::Exception($message); } } ?>
Errors in PHP
In PHP there may be three types of errors:
Notice: These types of errors are not serious problems; they are suppressed unless the logging level is changed in the php.ini file.
<">Warning: These types of errors failed code but does not terminate execution, but the script continues to run.
Fatal errors: These types of errors are serious errors, in these situations the script is unable to run. The fatal error terminates the script.
<?php function calculate_area($length,$breadth) { if($length<0 || $breadth<0) { throw new Exception("Invalid values"); } else { return $length*$breadth; } } $length=12; $breadth=-5; echo "Area of rectangle is: "; echo calculate_area($length, $breadth); ?>
In the above code we want to calculate the area of rectangle. If the value of length or breadth is negative then it will throw exception as follows:
If we call function with positive values (12 and 5) then we will get the result: as follows