PHP preg_match_all() Funksie
Voorbeeld
Vind alle voorkoms van "ain" in 'n string:
<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern =
"/ain/i";
if(preg_match_all($pattern, $str, $matches)) {
print_r($matches);
}
?>
Definisie en gebruik
Die preg_match_all()
funksie gee die aantal passings van 'n patroon terug wat in 'n string gevind is en vul 'n veranderlike in met die passings wat gevind is.
Sintaksis
preg_match_all(pattern, input, matches, flags, offset)
Parameterwaardes
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. One of the following structures may be selected:
|
offset | Optional. Defaults to 0. Indicates how far into the string to begin searching. The preg_match() function will not find matches that occur before the position given in this parameter |
Tegniese besonderhede
Terugkeerwaarde: | Wys die aantal passings gevind of vals as 'n fout voorgekom het |
---|---|
PHP weergawe: | 4+ |
Veranderinglog: | PHP 7.2 - Bygevoeg die PREG_UNMATCHED_AS_NULL-vlag PHP 5.4 - Die pasmaats-parameter het opsioneel geword PHP 5.3.6 - Die funksie gee terug as die offset langer is as die lengte van die invoer PHP 5.2.2 - Benoemde subpatrone kan die (?'naam' gebruik ) en (? <naam>) sintaksis bykomend tot die vorige (?P<naam>) |
Meer voorbeelde
Voorbeeld
Gebruik PREG_PATTERN_ORDER om die struktuur van die passings - skikking te stel. In hierdie voorbeeld het elke element in die passings -skikking al die passings vir een van die groeperings van die gereelde uitdrukking.
<?php
$str = "abc ABC";
$pattern = "/((a)b)(c)/i";
if(preg_match_all($pattern,
$str, $matches, PREG_PATTERN_ORDER)) {
print_r($matches);
}
?>
❮ PHP RegExp-verwysing