PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




preg_split

Name

preg_split — Splits the subject string along the boundaries defined by the specified regular expression pattern.

Synopsis

array preg_split(pattern, subject[, limit][, flags]);
string pattern: Regex pattern by which to split
string subject: Input string to split
int limit (optional): Limits the number of results
int flags (optional): Flags controlling the split process

Returns

Array containing the results of splitting.

Description

When 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.

PREG_SPLIT_NO_EMPTY

This flag specifies that only non-empty pieces should be returned by the function. Normally, empty pieces may be returned if the pattern matches at the beginning or the end of the string or if it matches two pieces next to each other.



If the string needs to be split by a static string, it's faster to use explode() .

Version

Existing since version 3.0.9

See also

See also explode()

Example

Example 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.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.