CKTIS / PHP
preg_match()
Search for the first match. A match returns 1, absence returns 0, and an execution or compilation error returns false.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|falseSearch for the first match. A match returns 1, absence returns 0, and an execution or compilation error returns false. Element zero holds the full match. PREG_OFFSET_CAPTURE pairs each capture with its byte offset even when the pattern uses UTF-8. The input $offset also counts bytes. Starting inside a UTF-8 character under the u modifier returns false with PREG_BAD_UTF8_OFFSET_ERROR.
Signature
preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false
Example output
[1,[["A42",3],["42",4]],0]
[false,true]
Examples
<?php
$input="é A42"; $found=preg_match("~A(\\d+)~u",$input,$m,PREG_OFFSET_CAPTURE);
echo json_encode([$found,$m,preg_match("~Z~",$input)]);
$bad=preg_match("~.~u","é",$matches,0,1); echo "\n",json_encode([$bad,preg_last_error()===PREG_BAD_UTF8_OFFSET_ERROR]);
Result
[1,[["A42",3],["42",4]],0] [false,true]