Introduction to PHP preg_match_all
PHP preg_match_all is a search-based function in the PHP programming language. It returns the total number of the matches found in a string. We can also see that it uses the regular expression pattern as a parameter to complete the searching process. This function is very while we must extract the phone number from a given string which also includes some textual information on that string. We can perform various other matching using this built-in PHP function. It gives the all matching as an array output as preg_match_all includes all in its name (suffix). preg_match() is the singular version of this function that gives only a single finding as an output.
Syntax and Parameter of PHP preg_match_all
Syntax of PHP preg_match_all are given below:
preg_match_all(regexPattern, inputString, matches, flags, offset)
Parameter:
- regexPattern: it is a required parameter. This is a regular expression itself we would like to move to use in the searching process.
- inputString: String upon which the whole operation depends. it is a required parameter.
- Matches: It is an optional parameter. This will give us the array containing all the matches in this. That means in this parameter variable the output will be stored.
- Flags: Again the optional one. It is all about how the search structures will be framed. It could be PREG_PATTERN_ORDER, PREG_SET_ORDER, PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL, etc.
- Offset: Again the optional one. The default will be 0.
Return type
The return type of this function will be match found or not. But at the same time, we can also see the output array of the finding in the function parameter itself in case we need that.
How does preg_match_all Work in PHP?
Before moving ahead with the preg_match_all, we must have a strong first upon which this function will be applied and a regular expression pattern. Beginners usually consider a bit complex to move ahead with the regular expression, but it is not. We can also get whether the pattern matches in the given string or not as well the output array of the findings.
The user of preg_match_all()
$string = "WELCOME TO INDIA"; // string
$patternRex = "/E/i"; // regular expression or the pattern
if(preg_match_all($patternRex, $string, $matches)) {
echo "<pre>";
print_r($matches);
}
In the above line of the code, we are searching for ‘E’ character. ‘i’ is being used in the regular expression for the not case sensitive. We will explore and see various other examples in the example section of this article.
Examples of PHP preg_match_all()
Following are examples of the php preg_match_all are given below:

4.5 (9,674 ratings)
View Course
Example #1
Searching ‘E’ character in the given string irrespective of the case of that string.
Code:
<?php
$string = "WELCOME TO INDIA."; // string
$patternRex = "/E/i"; // regular expression or the pattern
$matchFound = preg_match_all($patternRex, $string, $matches);
if($matchFound){
echo "<pre>";
print_r($matches);
}
?>
Output:
Example #2
Checked whether ‘E’ character (capital letter of E) exists in the given string or not.
Code:
<?php
$string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."; // string
$patternRex = "/E/"; // regular expression or the pattern
$matchFound = preg_match_all($patternRex, $string, $matches);
if($matchFound){
echo "<pre>";
echo "Match Found.";
print_r($matches);
}else{
echo "No match found.";
}
?>
Output:
The ‘e’ character exists, as we can see in the string of the above example code but the case is not matching as per the written in the pattern. No match found will be output but if we change the pattern to search for in-case sensitive it will give some output.
Example #3
Extracting all phone numbers from a given string.
Code:
<?php
$string = "Hi, please call us at 800-222-1212 or 800-001-1212 or 700-555-1212 or 800-555-1312"; // string
$patternRex = "/\b\d{3}\s*-\s*\d{3}\s*-\s*\d{4}\b/"; // regular expression or the pattern
$matchFound = preg_match_all($patternRex, $string, $matches);
if($matchFound){
echo "<pre>";
echo "Match Found.<br>";
print_r($matches);
}else{
echo "No match found.";
}
?>
Output:
Example #4
List the HTML <b> tag element of a given string of various tags.
Code:
<?php
$userinfo = "Name: <b>John Raw</b> Title: <b>PHP Lover</b>
Name: <b>Shree Sham</b> Title: <b>JAVA Lover</b>
Name: <b>Jonathan</b> Title: <b>Node Lover</b>
Name: <b>Amla Palekar </b> Title: <b>Comic Reader</b>";
preg_match_all ("/<b>(.*)<\/b>/U", $userinfo, $matches);
for ($i=0; $i< count($matches[0]); $i++) {
echo $matches[0][$i]."<br>";
}
?>
Output:
As we can see in the above example code, the code extracts all the tag with its value of the <b> tag. <b> usually used for making the string bold in the HTML. In the same way, we can try with other elements of the combination of elements to get the desired result as per our business requirements.
Example #5
A program to count the number of spaces of a given string using the preg_match_all() function.
Code:
<?php
$userinfo = "The code below count the number of spaces of the given string.";
$search = preg_match_all ("/\\s/", $userinfo, $matches);
if($search){
echo "Number of spaces: ".count($matches[0]);
}
?>
As we can see in the above line of code, we have a string containing spaces. We have a regular expression to match the pattern of the space. If the matches found we can see all the spaces inside the $matches array. Then we are using the count() function to get the count of that $matches array. There are various other ways we can use this PHP pattern-based PHP search function.
Output:
Conclusion
Built-in function preg_match_all() can be used to search anything in a string using the regular expression. This function not just gives the search found or not but also gives the search founding as an array. A developer or the coder can use this regular expression function to fulfill their various need as per their business requirements. A developer should be careful enough while using this as sometimes it gives multi-dimensional array as an output of the matching array.
Recommended Articles
This is a guide to PHP preg_match_all. Here we also discuss the introduction and how does preg_match_all works in php along with different examples and its code implementation. You may also have a look at the following articles to learn more –