Page 601 - Beginning PHP 5.3
P. 601
Chapter 18: String Matching with Regular Expressions
preg_split() takes string splitting a stage further by letting you specify a regular expression for the
delimiter. This gives you a lot more 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. Consider the following example:
$text = “John Steinbeck, Franz Kafka and J.R.R. Tolkien”;
$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 this result:
Array
(
[0] = > John Steinbeck
[1] = > Franz Kafka
[2] = > J.R.R. Tolkien
)
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() ’ s behavior by passing some
optional flags as the fourth argument:
❑ PREG_SPLIT_NO_EMPTY : 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 : 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() ’ s 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 — for example: PREG_SPLIT_NO_EMPTY |
PREG_SPLIT_DELIM_CAPTURE .
If you want to set one or more flags and don ’ t want to limit the number of elements returned, pass – as
1
the third argument.
To see how useful PREG_SPLIT_NO_EMPTY can be, consider the following example:
$text = “’hello’, ‘goodbye’”;
$letters = preg_split( “/[‘, ]/”, $text );
echo “ < pre > ”;
print_r( $letters );
echo “ < /pre > ”;
563
9/21/09 6:18:01 PM
c18.indd 563
c18.indd 563 9/21/09 6:18:01 PM