Funkce PHP preg_match_all().
❮ Referenční příručka PHP RegExp
Příklad
Najděte všechny výskyty "ain" v řetězci:
<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern =
"/ain/i";
if(preg_match_all($pattern, $str, $matches)) {
print_r($matches);
}
?>
Definice a použití
Funkce preg_match_all()
vrátí počet shod vzoru, které byly nalezeny v řetězci, a naplní proměnnou nalezenými shodami.
Syntax
preg_match_all(pattern, input, matches, flags, offset)
Hodnoty parametrů
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 |
Technické údaje
Návratová hodnota: | Vrátí počet nalezených shod nebo false, pokud došlo k chybě |
---|---|
Verze PHP: | 4+ |
Seznam změn: | PHP 7.2 – Přidán příznak PREG_UNMATCHED_AS_NULL PHP 5.4 – Parametr shody se stal volitelným PHP 5.3.6 – Funkce vrací false, když je offset delší než délka vstupu PHP 5.2.2 – Pojmenované podvzory mohou používat (?'name' ) a (? <name>) syntaxe navíc k předchozí (?P<name>) |
Další příklady
Příklad
Pomocí PREG_PATTERN_ORDER nastavte strukturu pole shod . V tomto příkladu má každý prvek v poli shody všechny shody pro jedno ze seskupení regulárního výrazu.
<?php
$str = "abc ABC";
$pattern = "/((a)b)(c)/i";
if(preg_match_all($pattern,
$str, $matches, PREG_PATTERN_ORDER)) {
print_r($matches);
}
?>
❮ Referenční příručka PHP RegExp