CKTIS / PHP
preg_split()
Découpe une chaîne aux correspondances du motif.
Version : PHP 8.4; repository runtime, production runtime not attested
Syntaxe
preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array|falseDécoupe une chaîne aux correspondances du motif. PREG_SPLIT_NO_EMPTY élimine les morceaux vides et PREG_SPLIT_DELIM_CAPTURE conserve les séparateurs capturés. Une limite positive laisse le reste entier dans le dernier morceau. Une erreur de motif renvoie false.
Signature
preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array|false
Sortie de l’exemple
[["a","b,c"],["a",",,","b",",","c"],["a","b","c"]]
Exemples
<?php
$s="a,,b,c"; echo json_encode([preg_split("~,+~",$s,2),preg_split("~(,+)~",$s,-1,PREG_SPLIT_DELIM_CAPTURE),preg_split("~,~",$s,-1,PREG_SPLIT_NO_EMPTY)]);
Résultat
[["a","b,c"],["a",",,","b",",","c"],["a","b","c"]]