The regular expression is a particular pattern that can be used to match, search, and replace any particular string in text data. It is a powerful feature of any programming language. It is mainly used to perform different types of form validations, such as validating email, checking the phone format, validating the username and password fields of the login form, etc. Any particular content of the text can be search and replaced using a regular expression pattern. It is also called regex or RegExp. Different types of symbols are used to define the regex pattern.
Many built-in functions exist in PHP for the regular expression. preg_match() function is one of them. This function is used for matching a particular pattern in string data. How the preg_match() function can be used in PHP for performing various tasks is explained in this tutorial.
Syntax:
This function can take five arguments. The first argument, $pattern, is mandatory, which defines the pattern for matching. The second argument, $subject, is mandatory, and contains the string data in which pattern will be applied. The third argument, $matches, is optional and it returns an array based on the match. The fourth argument, $flags, is optional, which contains different types of flag values based on the match. The fifth argument, $offset, is optional, and can be used to define the starting position of the search.
Example 1: Match the pattern in a case-sensitive and case-insensitive way
The following example shows the use of the preg_match() function for matching the string in a case-sensitive and case-insensitive manner. Create a PHP file with the following script.
Here, three patterns are used for applying in three preg_match() functions. The first pattern, ‘/Like/’, is used for matching the string in a case-sensitive way. The second pattern, ‘/like/’, is used for matching the string in a case-sensitive way. The third pattern, ‘Like/i’, is used for matching the string in a case-insensitive way. search_pattern() function is defined in the script to use preg_match() function for matching, and it prints the message based on the returned value of this function.
//Define the text
$text = "I like PHP. I like JavaScript also.";
//Define three types of pattern
$pattern1 = '/Like/';
$pattern2 = '/like/';
$pattern3 = '/Like/i';
//Define function for searching the pattern in the text
function search_pattern($pattern, $string)
{
if(preg_match($pattern, $string))
echo "Search result: <b> Match is found for the pattern - $pattern </b><br />";
else
echo "Search result: <b> Match is not found for the pattern - $pattern </b><br />";
}
//Print the original text
echo "The original text is: <b> $text </b><br />";
//Call the function three times for three patterns
search_pattern($pattern1, $text);
search_pattern($pattern2, $text);
search_pattern($pattern3, $text);
?>
Output:
The following output will appear after running the script from the server. The first line shows the text where the patterns were searched. The second line shows the output of the first pattern. The third line shows the output of the second pattern. The fourth line shows the output of the third pattern.
Example 2: Validate the URL
The following example shows the way to validate the URL address using the preg_match() function. Create a PHP file with the following script.
An URL value is assigned in the variable $url for testing. ‘@^(?:https://)?([^/]+)@i’ string is used as a pattern in the preg_match() function to check if the URL address is valid or not. If it is valid, then the hostname and the domain name will be printed, otherwise, the error message will be printed.
//Define the URL
$url = "https://ift.tt/2mBB8je";
//Define the pattern for validating the URL
$pattern ='@^(?:https://)?([^/]+)@i';
//Check the URL is valid or not
if(preg_match($pattern, $url, $matches1))
{
//Print the success message
echo "The URL is valid.<br />";
//Print the array that contains the match values
echo "The values of the array is: ";
print_r($matches1);
//Retrieve and print the host value
$host = $matches1[1];
echo "<br />Host name is: $host";
//Search the domain name from the host value
preg_match('/[^.]+\.[^.]+$/', $host, $matches2);
echo "<br />Domain name is: {$matches2[0]}";
}
else
{
//Print the error message
echo "Invalid URL.";
}
?>
Output:
The following output will appear after running the script from the server. The provided URL in the script is valid. So, the output shows the values of the $matches, the hostname of the URL, and the domain name of the URL.
Example 3: Search pattern with flag and offset values
The following example shows the uses of the flag and offset values in the preg_match() function. Create a PHP file with the following script.
Three patterns are used in the script for three types of matching. In the first preg_match() function, ‘/(bangla)(desh)/’ is used as the pattern and the flag value, PREG_OFFSET_CAPTURE is used. The values of the $matches variable will print based on the output of the first preg_match() function. In the second preg_match() function, ‘/(bangla)(glad)*(desh)/i’, is used as the pattern and the flag value, PREG_UNMATCHED_AS_NULL is used. If any part of the pattern does not match then a NULL value will be stored in the array. The values of the $matches variable will print based on the output of the second preg_match() function. In the third preg_match() function, ‘/glad/’ is used as the pattern, the flag value, PREG_OFFSET_CAPTURE is used and 3 is used as the offset value. The values of the $matches variable will print based on the output of the third preg_match() function.
//Define the text value
$text = "Bangladesh";
//Define three type of patterns
$pattern1 = '/(bangla)(desh)/i';
$pattern2 = '/(bangla)(glad)*(desh)/i';
$pattern3 = '/glad/';
//Using PREG_OFFSET_CAPTURE flag
preg_match($pattern1, $text, $matches, PREG_OFFSET_CAPTURE);
echo "<pre>";
print_r($matches);
echo "</pre><br />";
//Using PREG_UNMATCHED_AS_NULL flag
preg_match($pattern2, $text, $matches, PREG_UNMATCHED_AS_NULL);
echo "<pre>";
print_r($matches);
echo "</pre><br />";
//Using PREG_OFFSET_CAPTURE flag and offset value
preg_match($pattern3, $text, $matches, PREG_OFFSET_CAPTURE, 3);
echo "<pre>";
print_r($matches);
echo "</pre>";
?>
Output:
The following output will appear after running the script from the server.
Conclusion
The uses of the preg_match() function have been explained in this tutorial using multiple examples. The uses of different arguments of this function have also been shown here. The readers will be able to use this function properly in their script after reading this tutorial.
from Linux Hint https://ift.tt/3s0160Z
0 Comments