← All built-ins / preg_match()

preg_match()

preg_match(...$args): mixed

Parameters

NameTypeDescription
$patternstring/…/ pattern with optional flags i, s, m
$subjectstringstring to search
$matchesarrayout-parameter: filled with the match (index 0) and capture groups (1..n)optional

Return value

1 and fills $matches on success; false on no match or invalid pattern.

Example

$n = preg_match("/(\w+)@(\w+)/", "mail me: ada@holy.php");
echo $n, "
";
$m = null;
preg_match("/(\w+)@(\w+)/", "mail me: ada@holy.php", $m);
echo $m[1], " ", $m[2], "
";
1
ada holy

See also

preg_match_all()preg_replace()preg_split()