Funkce PHP preg_match().
❮ Referenční příručka PHP RegExp
Příklad
Použijte regulární výraz k vyhledávání „w3schools“ v řetězci bez ohledu na velikost písmen:
<?php
$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo
preg_match($pattern, $str);
?>
Definice a použití
Funkce preg_match()
vrátí, zda byla v řetězci nalezena shoda.
Syntax
preg_match(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:
|
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í 1, pokud byla nalezena shoda, 0, pokud nebyly nalezeny žádné shody, a 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.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<jméno>) |
Další příklady
Příklad
Použijte PREG_OFFSET_CAPTURE k nalezení pozice ve vstupním řetězci, ve kterém byly nalezeny shody:
<?php
$str = "Welcome to W3Schools";
$pattern = "/w3schools/i";
preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
❮ Referenční příručka PHP RegExp