search for in the  
<str_shufflestr_word_count>
Last updated: Thu, 19 May 2005

str_split

(PHP 5)

str_split --  Convert a string to an array

Description

array str_split ( string string [, int split_length] )

Converts a string to an array. If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length.

FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element.

Example 1. Example uses of str_split()

<?php

$str
= "Hello Friend";

$arr1 = str_split($str);
$arr2 = str_split($str, 3);

print_r($arr1);
print_r($arr2);

?>

Output may look like:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => F
    [7] => r
    [8] => i
    [9] => e
    [10] => n
    [11] => d
)

Array
(
    [0] => Hel
    [1] => lo 
    [2] => Fri
    [3] => end
)

Example 2. Examples related to str_split()

<?php

$str
= "Hello Friend";

echo
$str{0};  // H
echo $str{8};  // i

// Creates: array('H','e','l','l','o',' ','F','r','i','e','n','d')
$arr1 = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);

?>

See also chunk_split(), preg_split(), split(), count_chars(), str_word_count(), and for.



User Contributed Notes
str_split
shugotenshi at gmail dot com
19-Apr-2005 09:58
It's best not to overcomplicate things with redundant PHP-side loops and start taking into consideration of preg_split() to simulate str_split()'s behavior for < PHP 5 versions. http://www.php.net/preg-split has an example of how to split strings by one character, but with a little regex knowledge you can easily use a quantifier to split by as many characters as you want.
Sdb at hl2-bg dot net
22-Mar-2005 06:25
I made a simulation of that function:

function myStringSplit($string, $long){

   $long--;
   // Converting the string into an array
   For( $i=0; isset($string{$i}); $i++){
       $myarray[] = $string{$i};
   }

   // Making arrays from the array
   $a=0;
   $end = $long;
   $iend = count($myarray) / ( $long + 1 );
   For( $i=0; $i<$iend; $i++){

       For( ; $a<=$end; $a++){

           $array[$i] = $array[$i].$myarray[$a];
       }
       $end = $end + $long + 1;
   }
  
   return $array;
}

$test = "123456789";
$splited = myStringSplit2($test, '3');
print_r($splited);

Should return this:
Array (
[0] => 123
[1] => 456
[2] => 789
)

btw: It's mixed-up but it works :)
kelsey_requiem
08-Mar-2005 11:09
As this function is only available in php5, I had problems with it's availability, so i had to write my own. Here's the code:

<?php
function stringsplit($the_string, $the_number)
{
  
$startoff_nr = 0;
  
$the_output_array = array();
   for(
$z = 1; $z < ceil(strlen($the_string)/$the_number)+1 ; $z++)
   {   
      
$startoff_nr = ($the_number*$z)-$the_number;
      
$the_output_array[] = substr($the_string, $startoff_nr, $the_number);
   }
   return(
$the_output_array);
}

$string = "so long and thanks for all the fish!";
$foo = stringsplit($string, 6);

print(
$foo);
?>

and this will output:

Array
(
   [0] => so lon
   [1] => g and
   [2] => thanks
   [3] =>  for a
   [4] => ll the
   [5] =>  fish!
)

what is exactly the same as str_split() 's output. hope this solves someones problems.
lamtd at hotmail dot com
07-Mar-2005 06:20
The code from the post below is actually broken, a working version would be:

<?
function strsplit($str)
{
   if (!
preg_match_all("/./", $str, $split))
   {
       return
false;
   }
   return
$split[0];
}
?>

i.e. use preg_match_all() instead of preg_match(). At least it worked for me.
aidan at php dot net
20-May-2004 11:53
This functionality is now implemented in the PEAR package PHP_Compat.

More information about using this function without upgrading your version of PHP can be found on the below link:

http://pear.php.net/package/PHP_Compat

<str_shufflestr_word_count>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT