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

❮ Référence PHP RegExp

Exemple

Utilisez preg_split() pour diviser une date en ses composants :

<?php
$date = "1970-01-01 00:00:00";
$pattern = "/[-\s:]/";
$components = preg_split($pattern, $date);
print_r($components);
?>

Définition et utilisation

La preg_split()fonction sépare une chaîne dans un tableau en utilisant les correspondances d'une expression régulière comme séparateurs.


Syntaxe

preg_split(pattern, string, limit, flags)

Valeurs des paramètres

Parameter Description
pattern Required. A regular expression determining what to use as a separator
string Required. The string that is being split
limit Optional. Defaults to -1, meaning unlimited. Limits the number of elements that the returned array can have. If the limit is reached before all of the separators have been found, the rest of the string will be put into the last element of the array
flags Optional. These flags provide options to change the returned array:
  • PREG_SPLIT_NO_EMPTY - Empty strings will be removed from the returned array.
  • PREG_SPLIT_DELIM_CAPTURE - If the regular expression contains a group wrapped in parentheses, matches of this group will be included in the returned array.
  • PREG_SPLIT_OFFSET_CAPTURE - Each element in the returned array will be an array with two element, where the first element is the substring and the second element is the position of the first character of the substring in the input string.

Détails techniques

Valeur de retour : Renvoie un tableau de sous-chaînes où chaque élément correspond à une partie de la chaîne d'entrée séparée par une correspondance de l'expression régulière
Version PHP : 4+

Plus d'exemples

Exemple

Utilisation de l'indicateur PREG_SPLIT_DELIM_CAPTURE :

<?php
$date = "1970-01-01 00:00:00";
$pattern = "/([-\s:])/";
$components = preg_split($pattern, $date, -1,
PREG_SPLIT_DELIM_CAPTURE);
print_r($components);
?>

Exemple

Utilisation de l'indicateur PREG_SPLIT_OFFSET_CAPTURE :

<?php
$date = "1970-01-01";
$pattern = "/-/";
$components = preg_split($pattern, $date, -1,
PREG_SPLIT_OFFSET_CAPTURE);
print_r($components);
?>

❮ Référence PHP RegExp