Tutoriel PHP

ACCUEIL PHP Introduction PHP Installation PHP Syntaxe PHP Commentaires PHP Variables PHP Écho PHP / Impression Types de données PHP Chaînes PHP Numéros PHP Mathématiques PHP Constantes PHP Opérateurs PHP PHP Si... Sinon... Sinon Commutateur PHP Boucles PHP Fonctions PHP Tableaux PHP Superglobales PHP Expression régulière PHP

Formulaires PHP

Gestion des formulaires PHP Validation de formulaire PHP Formulaire PHP requis URL/courriel du formulaire PHP Formulaire PHP terminé

PHP Avancé

Date et heure PHP Inclure PHP Gestion des fichiers PHP Fichier PHP Ouvrir/Lire Création/écriture de fichier PHP Téléchargement de fichier PHP Cookies PHP Séances PHP Filtres PHP Filtres PHP avancés Fonctions de rappel PHP PHPJSON Exception PHP

POO PHP

PHP Qu'est-ce que la POO Classes/Objets PHP Constructeur PHP Destructeur PHP Modificateurs d'accès PHP Héritage PHP Constantes PHP Classes abstraites PHP Interface PHP Caractéristiques PHP Méthodes statiques PHP Propriétés statiques PHP Espaces de noms PHP Itérables PHP

Base de données MySQL

Base de données MySQL Connexion MySQL Créer une base de données MySQL Créer une table MySQL MySQL Insérer des données MySQL obtenir le dernier ID MySQL Insérer plusieurs MySQL préparé MySQL Sélectionner les données MySQL Où Trier MySQL par MySQL Supprimer les données Données de mise à jour MySQL Données de limite MySQL

XML PHP

Analyseurs PHP XML Analyseur PHP SimpleXML PHP SimpleXML - Obtenir Expatriation PHP XML PHP XML DOM

PHP -AJAX

Introduction à AJAX PHP AJAX Base de données AJAX XML AJAX Recherche en direct AJAX Sondage AJAX

Exemples PHP

Exemples PHP Compilateur PHP Questionnaire PHP Exercices PHP Certificat PHP

Référence PHP

Présentation de PHP Tableau PHP Calendrier PHP Date PHP Annuaire PHP Erreur PHP Exception PHP Système de fichiers PHP Filtre PHP FTP PHP PHPJSON Mots clés PHP PHP LibxmlComment Messagerie PHP Mathématiques PHP Divers PHP PHP MySQL Réseau PHP Contrôle de sortie PHP Expression régulière PHP PHP SimpleXML Flux PHP Chaîne PHP Gestion des variables PHP Analyseur PHP XML Code postal PHP Fuseaux horaires PHP

Fonction PHP preg_match()

❮ Référence PHP RegExp

Exemple

Utilisez une expression régulière pour effectuer une recherche insensible à la casse pour "w3schools" dans une chaîne :

<?php
$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo preg_match($pattern, $str);
?>

Définition et utilisation

La preg_match()fonction renvoie si une correspondance a été trouvée dans une chaîne.


Syntaxe

preg_match(pattern, input, matches, flags, offset)

Valeurs des paramètres

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:
  • PREG_OFFSET_CAPTURE - When this option is enabled, each match, instead of being a string, will be an array where the first element is a substring containing the match and the second element is the position of the first character of the substring in the input.
  • PREG_UNMATCHED_AS_NULL - When this option is enabled, unmatched subpatterns will be returned as NULL instead of as an empty string.
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

Détails techniques

Valeur de retour : Renvoie 1 si une correspondance a été trouvée, 0 si aucune correspondance n'a été trouvée et false si une erreur s'est produite
Version PHP : 4+
Journal des modifications : PHP 7.2 - Ajout du drapeau PREG_UNMATCHED_AS_NULL

PHP 5.3.6 - La fonction renvoie false lorsque le décalage est plus long que la longueur de l'entrée

PHP 5.2.2 - Les sous-modèles nommés peuvent utiliser le (?'nom') et (? <nom>) syntaxe en plus de la précédente (?P<nom>)

Plus d'exemples

Exemple

Utilisez PREG_OFFSET_CAPTURE pour trouver la position dans la chaîne d'entrée dans laquelle les correspondances ont été trouvées :

<?php
$str = "Welcome to W3Schools";
$pattern = "/w3schools/i";
preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

❮ Référence PHP RegExp