CKTIS / PHP
preg_match()
Cerca la prima corrispondenza.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|falseCerca la prima corrispondenza. Restituisce 1 se la trova, 0 se è assente e false in caso di errore di compilazione o esecuzione. L’indice zero contiene la corrispondenza completa. PREG_OFFSET_CAPTURE aggiunge la posizione in byte anche in UTF-8. Anche $offset conta byte. Con il modificatore u una posizione interna a un carattere UTF-8 dà false e PREG_BAD_UTF8_OFFSET_ERROR.
Firma
preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false
Output dell’esempio
[1,[["A42",3],["42",4]],0]
[false,true]
Esempi
<?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]);
Risultato
[1,[["A42",3],["42",4]],0] [false,true]