preg_splitNamepreg_split — Splits the subject string along the boundaries defined by the specified regular expression pattern.DescriptionWhen it's necessary to split a string with a dynamic expression rather than a fixed one, this function comes to the rescue. The basic underlying idea is the same as preg_match_all() except that, instead of returning matched pieces of the subject string, it returns an array of pieces that didn't match the specified pattern . The optional parameter limit specifies how many pieces to return; if -1 or omitted, all pieces are returned. Otherwise, exactly limit pieces are returned, with the last piece containing the remainder of the subject string that was not split. The limit value of -1 is useful to specify the optional flags parameter without setting a hard limit on the number of pieces returned. flags can be a combination of the following constants (combined with |, the bitwise OR operator). PHP 4.0.4.pl1 has only one such constant, but future versions will have more.
If the string needs to be split by a static string, it's faster to use explode() . ExampleExample 1134. Split a sentence into simple words
$sentence = "Famous expression: dum spiro, spero."; $words = preg_split('/[^a-zA-Z\'"-]+/', $sentence, -1, PREG_SPLIT_NO_EMPTY); foreach($words as $word) { echo "word: $word\n"; }This example produces the following output: word: Famous word: expression word: dum word: spiro word: spero
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|