PHP preg_match_all() Function
The preg_match_all() function returns the number of matches of a pattern that were found in a string and populates a variable with the matches that were found.
Syntax
Parameter Values
Parameter | Description |
---|---|
pattern | Required. Contains a regular expression indicating what to search for |
input | Required. The string in which the search will be performed |
matches | Optional. The variable used in this parameter will be populated with an array containing all of the matches that were found |
flags | Optional. A set of options that change how the matches array is structured. |
- PREG_PATTERN_ORDER — Default. Each element in the matches array is an array of matches from the same grouping in the regular expression, with index 0 corresponding to matches of the whole expression and the remaining indices for subpattern matches.
- PREG_SET_ORDER — Each element in the matches array contains matches of all groupings for one of the found matches in the string.
- PREG_OFFSET_CAPTURE — When this option is enabled, each match, instead of being a string, will be an array where the first element is a substring containing the match and the second element is the position of the first character of the substring in the input.
- PREG_UNMATCHED_AS_NULL — When this option is enabled, unmatched subpatterns will be returned as NULL instead of as an empty string.
Technical Details
Return Value: | Returns the number of matches found or false if an error occurred |
---|---|
PHP Version: | 4+ |
Changelog: | PHP 7.2 — Added the PREG_UNMATCHED_AS_NULL flag |
PHP 5.4 — The matches parameter became optional
PHP 5.3.6 — The function returns false when the offset is longer than the length of the input
More Examples
Example
Use PREG_PATTERN_ORDER to set the structure of the matches array. In this example, each element in the matches array has all of the matches for one of the groupings of the regular expression.
$str = «abc ABC»;
$pattern = «/((a)b)(c)/i»;
if(preg_match_all($pattern, $str, $matches, PREG_PATTERN_ORDER)) print_r($matches);
>
?>?php
Preg matching and counting the resulting match in a short string
I already have a function that counts the number of items in a string ($paragraph) and tells me how many characters the result is, ie tsp and tbsp present is 7, I can use this to work out the percentage of that string is.
I need to reinforce this with preg_match because 10tsp should count as 5.
$characters = strlen($paragraph); $items = array("tsp", "tbsp", "tbs"); $count = 0; foreach($items as $item) < //Count the number of times the formatting is in the paragraph $countitems = substr_count($paragraph, $item); $countlength= (strlen($item)*$countitems); $count = $count+$countlength; >$overallpercent = ((100/$characters)*$count);
I know it would be something like preg_match(‘#[d]+[item]#’, $paragraph) right?
EDIT sorry for the curve ball but there might be a space inbetween the number and the $item, can one preg_match catch both instances?
Answers
It’s not quite clear to me what you are trying to do with the regex, but if you are just trying to match for a particular number-measurement combination, this might help:
$count = preg_match_all('/d+s*(tbsp|tsp|tbs)/', $paragraph);
This will return the number of times a number-measurement combination occurs in $paragraph .
EDIT switched to use preg_match_all to count all occurrences.
Example for counting the number of matched characters:
$paragraph = "5tbsp and 10 tsp"; $charcnt = 0; $matches = array(); if (preg_match_all('/d+s*(tbsp|tsp|tbs)/', $paragraph, $matches) > 0) < foreach ($matches[0] as $match) < $charcnt += strlen($match); >> printf("total number of characters: %dn", $charcnt);
Output from executing the above:
total number of characters: 11