Base de connaissancesCentre d’examen
FRFrançais
Choisir la langue13

CKTIS / PHP

preg_match()

Recherche la première correspondance.

Version : PHP 8.4; repository runtime, production runtime not attested

Syntaxe

preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false

Recherche la première correspondance. Le résultat vaut 1 en cas de succès, 0 en l’absence de correspondance et false en cas d’erreur. L’indice zéro contient toute la correspondance. PREG_OFFSET_CAPTURE ajoute la position en octets, même en UTF-8. Le paramètre $offset compte aussi les octets. Avec le modificateur u, une position à l’intérieur d’un caractère UTF-8 donne false et PREG_BAD_UTF8_OFFSET_ERROR.

Signature

preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false

Sortie de l’exemple

[1,[["A42",3],["42",4]],0]
[false,true]

Exemples

<?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]);

Résultat

[1,[["A42",3],["42",4]],0]
[false,true]

Sources