[Previous] [Contents] [Next]


Splitting a string into an array


The following two functions split strings:

array split(string pattern, string source [, integer limit])
array spliti(string pattern, string source [, integer limit])

They split the source string into an array, breaking the string where the matching pattern is found. These functions perform a similar task to the explode( ) function described earlier and as with explode( ), a limit can be specified to determine the maximum number of elements in the array.

The following simple example shows how split( ) can break a sentence into an array of "words" by recognizing any sequence of nonalphabetic characters as separators:

$sentence = "I wonder why he does\nBuzz, buzz, buzz!";
$words = split("[^a-zA-Z]+", $sentence);

When complex patterns aren't needed to break a string into an array, the explode( ) function makes a better choice.


[Previous] [Contents] [Next]