Sunday, December 21, 2008

php explode convert string in to array

php explode

How to convert a string in to array in php?

PHP has lots of built in string functions. PHP also has a string function which easily split a string into an array with the help of repeatable delimiter. The function name is explode, with 3 parameters to it.

Syntax of explode.
array explode( string $delimiter,string $string,[int $limit]);

function description explode

explode simply returns an array of strings, where each element of an array is nothing but a substring of string provided in variable $string. String is split on the basis of $delimiter.

parameter description
1. string $delimiter
$delimiter can be a character or no of characters which occurs repeatedly in $string. It is nothing but a boundary for all substrings.

2.string $string
This is nothing but any string that is to be converted in array with the help of above delimiter.

Point to note.
1.What if delimiter is empty.
->If delimiter string is empty e.g. "" then explode() will simply return FALSE.

Example snippet.
$string = 'php||explode||returns||array';
$delimiter = '||'

output of above snippet.
[0] => php
[1] => explode
[2] => returns
[3] => array

Converse function of explode
implode() : post coming soon.