Introduction
Preg_split helps you break a string into parts using patterns, not just simple characters. It is useful when you want to split text by complex rules.
preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array
$parts = preg_split('/,/', 'apple,banana,orange');
$parts = preg_split('/\s+/', 'one two three');
$parts = preg_split('/[,.]/', 'red,green.orange');
$parts = preg_split('/\d+/', 'abc123def456ghi');
<?php $text = "apple, banana; orange.pear"; // Split by comma, semicolon, or dot with optional spaces $parts = preg_split('/\s*[,;.]+\s*/', $text); foreach ($parts as $part) { echo $part . "\n"; } ?>
preg_split do?preg_split is designed to split strings based on patterns, not fixed characters.preg_split functionality.preg_split?preg_split is the regex pattern as a string, the second is the input string./, /. preg_split('/,/', $string); uses this correctly.$text = "apple,banana;orange|grape";
$result = preg_split('/[,;|]/', $text);
print_r($result);/[,;|]/ matches any comma, semicolon, or pipe character as separators.$str = "one two three";
$parts = preg_split(' ', $str);
print_r($parts);preg_split.'/ /' to match a space.preg_split call achieves this?/[ ,]+/ matches one or more spaces or commas, so it splits on any group of these separators.PREG_SPLIT_NO_EMPTY tells preg_split to exclude empty strings from the result array.Pass the test and this pattern moves unknown