Here, you will study the final regular expression function preg_split() . The preg_split() Function takes string splitting. This gives you a lot of flexibility when deciding what to split a string on, and is very useful when you need to parse a string written in human – friendly form.
Example
$text = “Garima, Aman and Hemlata”;
$authors = preg_split( “/,\s*|\s+and\s+/”, $text );
echo “ < pre > ”;
print_r( $authors );
echo “ < /pre > ”;
This code splits up the input string into its individual author names. The regular expression matches either a comma followed by zero or more whitespace characters, or the word “and” surrounded by one or more whitespace characters. This means that, whenever one of these two patterns is found in the input string, the string is split at that point, producing the result:
Array
(
[0] = > Garima
[1] = > Aman
[2] = > Hemlata
)
As with explode(), you can limit the number of array elements returned by passing an integer as the third argument to preg_split(). You can also control preg_split() behavior by passing some optional flags as the fourth argument:
- PREG_SPLIT_NO_EMPTY: It removes any empty substrings from the returned array. This is useful for removing unwanted substrings, as you see in a moment.
- PREG_SPLIT_DELIM_CAPTURE: It causes any matched subpatterns in the delimiter expression to be returned in the array, as well as the string parts.
- PREG_SPLIT_OFFSET_CAPTURE: This works much like preg_match() PREG_OFFSET_CAPTURE flag. When set, preg_split() returns an array of arrays, where each nested array contains two elements the text of the extracted substring and its position in the original string.
To set multiple flags, combine them with the bitwise OR operator.
Example: PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE.
If you want to set one or more flags and do not want to limit the number of elements returned, pass – 1 as the third argument.
To see how PREG_SPLIT_NO_EMPTY , consider the example:
Code
$text = “’hello’, ‘goodbye’”;
$letters = preg_split( “/[‘, ]/”, $text );
echo “ < pre > ”;
print_r( $letters );
echo “ < /pre > ”;
Output
Array
(
[0] = >
[1] = > hello
[2] = >
[3] = >
[4] = >
[5] = > goodbye
[6] = >
)
This is because the regular expression causes any of the apostrophe, comma, and space characters to be treated as delimiters. So the string is split right at the start and end because the first and last characters are delimiters, and is also split three times between “ hello ” and “ goodbye ” because preg_split() “ sees ” three empty strings between the apostrophe, comma, and space characters in the input string.
Naturally these empty substrings are unwanted. By setting the PREG_SPLIT_NO_EMPTY flag you can easily remove these substrings from the resulting array:
Code
$text = “’hello’, ‘goodbye’”;
$letters = preg_split( “/[‘, ]/”, $text, -1, PREG_SPLIT_NO_EMPTY );
echo “ < pre > ”;
print_r( $letters );
echo “ < /pre > ”;
Output
Array
(
[0] = > hello
[1] = > goodbye
)