Showing posts with label Split function in PHP. Show all posts
Showing posts with label Split function in PHP. Show all posts

Friday, October 17, 2008

The str_split() function

This function splits a string into an array of strings.

syntax: str_split(string[,length]);

string – the input string
length – the number of characters each element in the output array must hold. The default value is 1.

Example input:

$str1="This is a simple program number";
print_r(str_split($str1,5));

Example output:

Array ( [0] => This [1] => is a [2] => simpl [3] => e pro [4] => gram [5] => numbe [6] => r )

Tuesday, September 23, 2008

The strtok() function

This function could be used to split a string into smaller strings.

Syntax: strtok(string,split);

split – the split character
Example input:
$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo $str2 = strtok($str1," ");
echo "
";
echo strtok(" ");
echo "
";
echo strtok(" ");
Example output:
This is a simple PHP program This is a
A good look at the example and you will realize that the string is specified only for the first time strtok() is called. For subsequent calls, the string need not be mentioned as the function keeps track of the current string.