PhpDig.net

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




explode

Name

explode — Splits a string into an array of substrings.

Synopsis

array explode(boundary, string[, limit]);
string boundary: String denoting the boundary between array elements
string string: String to parse into an array
int limit (optional): Maximum number of array elements to return

Returns

Array containing zero or more substrings of the string argument; FALSE on error or if the boundary argument is empty

Description

explode() is used to break a string into an array. The array is created by parsing the string from left to right. The first array element contains every character from the first character of the string to the first occurrence of the character(s) contained in the boundary argument. Subsequent array elements contain every character after the previous occurrence of the boundary character(s) to the next occurrence of the boundary character(s).

If the optional limit argument is used, parsing stops after the array contains limit elements. The last element contains the unparsed remainder of the string.

Version

PHP 3+, PHP 4+ (optional argument added in PHP 4.0.1)

See also

Other functions that can be used to parse a string into separate elements:

preg_split()

split()

unpack()

The POSIX-compatible and Perl-compatible regular expression functions

To create a string from an array:

implode()



Example

Example 1200. Demonstrate how explode() behaves

<?php
// Basic use of explode()
$string = "1||2||3";
var_dump(explode('||', $string));

// Show how explode() handles empty strings between boundary strings
$string = ";;";
var_dump(explode(';', $string));

// Try parsing CSV data
// Note that boundary strings inside quotes are *not* ignored
$string = '1,2,"a, b", 4';
var_dump(explode(',', $string));

// Use the optional limit argument
$string = '1,2,3,4,5,6';
var_dump(explode(',', $string, 3));
?>

Output:
array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
}
array(3) {
  [0]=>
  string(0) ""
  [1]=>
  string(0) ""
  [2]=>
  string(0) ""
}
array(5) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(2) ""a"
  [3]=>
  string(3) " b""
  [4]=>
  string(2) " 4"
}
array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(7) "3,4,5,6"
}



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.