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_all()

❮ Référence PHP RegExp

Exemple

Trouver toutes les occurrences de "ain" dans une chaîne :

<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern = "/ain/i";
if(preg_match_all($pattern, $str, $matches)) {
  print_r($matches);
}
?>

Définition et utilisation

La preg_match_all()fonction renvoie le nombre de correspondances d'un modèle trouvées dans une chaîne et remplit une variable avec les correspondances trouvées.


Syntaxe

preg_match_all(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.

One of the following structures may be selected:
  • PREG_PATTERN_ORDER - Default. Each element in the matches array is an array of matches from the same grouping in the regular expression, with index 0 corresponding to matches of the whole expression and the remaining indices for subpattern matches.
  • PREG_SET_ORDER - Each element in the matches array contains matches of all groupings for one of the found matches in the string.
Any number of the following options may be applied:
  • 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 le nombre de correspondances trouvées ou 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.4 - Le paramètre matches est devenu facultatif

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>) en plus de la syntaxe précédente (?P<nom>)

Plus d'exemples

Exemple

Utilisez PREG_PATTERN_ORDER pour définir la structure du tableau de correspondances . Dans cet exemple, chaque élément du tableau matches contient toutes les correspondances pour l'un des groupements de l'expression régulière.

<?php
$str = "abc ABC";
$pattern = "/((a)b)(c)/i";
if(preg_match_all($pattern, $str, $matches, PREG_PATTERN_ORDER)) {
  print_r($matches);
}
?>

❮ Référence PHP RegExp