CKTIS / PHP
preg_split()
Split text at regular-expression matches.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array|falseSplit text at regular-expression matches. PREG_SPLIT_NO_EMPTY drops empty pieces and PREG_SPLIT_DELIM_CAPTURE retains captured delimiters. A positive limit leaves the unsplit remainder in the last piece. A pattern error returns false.
Signature
preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array|false
Example output
[["a","b,c"],["a",",,","b",",","c"],["a","b","c"]]
Examples
<?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)]);
Result
[["a","b,c"],["a",",,","b",",","c"],["a","b","c"]]