PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
The preg_split()
function in PHP is used to split a string by a regular expression. This function is similar to the explode()
function, but it uses a regular expression to specify the delimiter.
Here's a basic tutorial on how to use the preg_split()
function in PHP:
Syntax:
The syntax of preg_split()
is:
preg_split(string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]]) : array|false
$pattern
: The pattern to search for, as a string.$subject
: The input string.$limit
: If specified, then only substrings up to limit
are returned with the rest of the string being placed in the last substring. A limit of -1, 0 or null means "no limit" and, as is standard across PHP, you can use null to skip to the flags
parameter.$flags
: PREG_SPLIT_NO_EMPTY
, PREG_SPLIT_DELIM_CAPTURE
, PREG_SPLIT_OFFSET_CAPTURE
could be used. If PREG_SPLIT_NO_EMPTY
is used, only non-empty pieces will be returned. PREG_SPLIT_DELIM_CAPTURE
includes the separators in the result. PREG_SPLIT_OFFSET_CAPTURE
will return for each item the offset where it was found in the input string in addition to the item.Return Value:
Returns an array containing substrings of subject
split along boundaries matched by pattern
, or FALSE on failure.
Example:
Here's an example demonstrating how to use preg_split()
:
<?php $pattern = '/[\s,]+/'; $subject = "apple, banana pear plum"; // using preg_split() $result = preg_split($pattern, $subject); // Output: Array ( [0] => apple [1] => banana [2] => pear [3] => plum ) print_r($result); ?>
In this example, the $pattern
is a regular expression that matches one or more spaces or commas. The preg_split()
function then splits the $subject
string at each match of the pattern, resulting in an array of the words in the string.
Delimiter Capture Example:
Here is an example of using preg_split()
with delimiter capture:
<?php $pattern = '/([, ]+)/'; $subject = "apple, banana pear plum"; // using preg_split() with PREG_SPLIT_DELIM_CAPTURE $result = preg_split($pattern, $subject, -1, PREG_SPLIT_DELIM_CAPTURE); // Output: Array ( [0] => apple [1] => , [2] => banana [3] => [4] => pear [5] => [6] => plum ) print_r($result); ?>
In this example, the $pattern
is a regular expression that matches one or more spaces or commas, and we've included the parentheses to capture the delimiters. The preg_split()
function then splits the $subject
string at each match of the pattern, but it also includes the delimiters in the result array because of the PREG_SPLIT_DELIM_CAPTURE
flag.
Splitting strings using regular expressions with preg_split()
in PHP:
preg_split()
is used to split a string into an array using a regular expression pattern.$pattern = "/[\s,]+/"; $text = "apple,banana orange"; $words = preg_split($pattern, $text);
Custom delimiters and pattern options in PHP preg_split()
:
$pattern = "~[\s,]+~"; $text = "apple,banana orange"; $words = preg_split($pattern, $text);
Handling multiple patterns and OR conditions with preg_split()
in PHP:
|
(pipe) symbol for OR conditions.$patterns = ["~[\s,]+~", "/;/"]; $text = "apple,banana;orange"; $words = preg_split($patterns, $text);
Limiting the number of splits with preg_split()
in PHP:
$pattern = "/[\s,]+/"; $text = "apple,banana orange"; $words = preg_split($pattern, $text, 2);
Case-insensitive splitting with preg_split()
in PHP:
i
flag for case-insensitive splitting.$pattern = "/[\s,]+/i"; $text = "Apple,Banana Orange"; $words = preg_split($pattern, $text);
Preserving and excluding delimiters in preg_split()
results:
$pattern = "/([\s,]+)/"; $text = "apple,banana orange"; $words = preg_split($pattern, $text, -1, PREG_SPLIT_DELIM_CAPTURE);
Error handling and debugging with preg_split()
in PHP:
preg_last_error()
.$pattern = "/[a-z/"; $text = "Invalid pattern"; $words = preg_split($pattern, $text); if ($words === false) { $error = preg_last_error(); echo "Error: $error"; }