PHP
How to Download & Install XAMPP on Windows: PHP Tutorial
What is XAMPP? XAMPP is an open-source, cross-platform web server that consists of a web server,...
PHP Regular Expression also known as regex are powerful pattern matching algorithm that can be performed in a single expression. Regular expressions use arithmetic operators such as (+,-,^) to create complex expressions. They can help you accomplish tasks such as validating email addresses, IP address etc.
Why use regular expressions
In this PHP Regex tutorial, you will learn:
PHP has built in functions that allow us to work with regular functions which we will learn in this PHP Regular Expressions tutorial. Let’s look at the commonly used regular expression functions in PHP.
Below is the syntax for a regular expression function such as PHP preg_match(), PHP preg_split() or PHP preg_replace().
<?php function_name('/pattern/',subject); ?>
HERE,
Let’s now look at practical examples that implement the above PHP regex functions.
The first example uses the preg_match() in PHP function to perform a simple pattern match for the word guru in a given URL.
The code below shows the implementation for preg_match() tester function for the above example.
<?php $my_url = "www.guru99.com"; if (preg_match("/guru/", $my_url)) { echo "the url $my_url contains guru"; } else { echo "the url $my_url does not contain guru"; } ?>
Browse to the URL http://localhost/phptuts/preg_match_simple.php
Let’s examine the part of the code responsible for our output "preg_match('/guru/', $my_url)" HERE,
The diagram below summarizes the above points
Let’s now look at another example that uses the preg_split() in PHP function.
We will take a string phrase and explode it into an array; the pattern to be matched is a single space.
The text string to be used in this example is "I Love Regular Expressions".
The code below illustrates the implementation of the above example.
<?php $my_text="I Love Regular Expressions"; $my_array = preg_split("/ /", $my_text); print_r($my_array ); ?>
Browse to the URL http://localhost/phptuts/preg_split.php
Let’s now look at the preg_replace() in PHP function that performs a pattern match and then replaces the pattern with something else.
The code below searches for the word guru in a string.
It replaces the word guru with the word guru surrounded by css code that highlights the background colour.
<?php $text = "We at Guru99 strive to make quality education affordable to the masses. Guru99.com"; $text = preg_replace("/Guru/", '<span style="background:yellow">Guru</span>', $text); echo $text; ?>
Assuming you have saved the file preg_replace.php, browser to the URL http://localhost/phptuts/preg_replace.php
The above examples used very basic patterns; metacharacters simply allow us to perform more complex pattern matches such as test the validity of an email address. Let’s now look at the commonly used metacharacters.
Metacharacter | Description | Example |
---|---|---|
. | Matches any single character except a new line | /./ matches anything that has a single character |
^ | Matches the beginning of or string / excludes characters | /^PH/ matches any string that starts with PH |
$ | Matches pattern at the end of the string | /com$/ matches guru99.com,yahoo.com Etc. |
* | Matches any zero (0) or more characters | /com*/ matches computer, communication etc. |
+ | Requires preceding character(s) appear at least once | /yah+oo/ matches yahoo |
\ | Used to escape meta characters | /yahoo+\.com/ treats the dot as a literal value |
[...] | Character class | /[abc]/ matches abc |
a-z | Matches lower case letters | /a-z/ matches cool, happy etc. |
A-Z | Matches upper case letters | /A-Z/ matches WHAT, HOW, WHY etc. |
0-9 | Matches any number between 0 and 9 | /0-4/ matches 0,1,2,3,4 |
The above list only gives the most commonly used metacharacters in regular expressions.
Let’s now look at a fairly complex example that checks the validity of an email address.
<?php
$my_email = "This email address is being protected from spambots. You need JavaScript enabled to view it.";
if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/", $my_email)) {
echo "$my_email is a valid email address";
}
else
{
echo "$my_email is NOT a valid email address";
}
?>
HERE,
Browse to the URL http://localhost/phptuts/preg_match.php
As you can see from the above example breakdown, metacharacters are very powerful when it comes to matching patterns.
What is XAMPP? XAMPP is an open-source, cross-platform web server that consists of a web server,...
PHP is an open-source server-side scripting language that is used to develop static or dynamic web...
What is Ajax? AJAX full form is Asynchronous JavaScript & XML. It is a technology that reduces the...
Following are frequently asked Laravel and PHP related interview questions for freshers as well as...
What is PHP? PHP is a server side scripting language. that is used to develop Static websites or...
What is a control structure? Code execution can be grouped into categories as shown below...