★ wanayoo — archive 1999 http://fr.php.net/manual/en/function.str-split.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<str_shufflestr_word_count>
view the version of this page
Last updated: Thu, 26 Feb 2004

str_split

(PHP 5 CVS only)

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.



add a note add a note User Contributed Notes
str_split
Paul Gregg / Qube #efnet
18-Nov-2003 01:07
A neat solution for older versions of PHP is:
http://www.pgregg.com/projects/php/code/str_split.phps
(I had wrote this as strsplit() before I found out str_split() existed, so I renamed mine):

/*
 * split a string up into equal sized chunks
 *
 * Paul Gregg <pgregg@pgregg.com>
 * 23 September 2003
 *
 * Open Source Code:  If you use this code on your site for public
 * access (i.e. on the Internet) then you must attribute the author and
 * source web site: http://www.pgregg.com/projects/php/code/str_split.phps
 * str_split is available from PHP version 5 by default
 *
 */

if (!function_exists('str_split')) {
  Function str_split($string, $chunksize=1) {
   preg_match_all('/('.str_repeat('.', $chunksize).')/Uims', $string, $matches)
;
   return $matches[1];
  }
}
NOSPAM at NOSPAM dot NOSPAM
08-Sep-2003 01:11
Again, an emulation of str_split for older versions just like the above, only in simpler code (should work with php3 and 4, I guess...):

function str_split($str,$num = '1') {

   if($num < 1) return FALSE;

   $arr = array();

   for ($j = 0; $j < strlen($str); $j= $j+$num) {

       $arr[] = substr($str,$j,$num);

   }

   return $arr;

}
msw_williams at hotmail dot com
08-Aug-2003 12:43
thanks for the jpm_split function it was a great help,

i found that the function was adding an extra separtor on the end, so i used the wordwrap function instead of chunk_split :)

function msw_split($string, $split_len=1, $separator=':')
{
  $str = wordwrap($string,$split_len,$separator,$split_len);
  $str_array = explode($separator,$str);
  return $str_array;
}
funnykindaguy at yahoo dot com
11-Jul-2003 04:53
The following function will do the exact same thing (from PHP 3.0.6 and up), is just as simple to use and only takes two lines!

function jpm_split($string, $split_length=1, $separator=':') {
  return explode($separator, chunk_split($string, $split_length, $separator));
}

It works the same way and has the same parameters with the addition of "separator". This optional parameter is necessary in case you know that your string already contains the default separator ':' in it. In which case you would simply change it to any other safe character in order for the function to work properly.

* In my previous submission I accidentally spelt "separator" as "seperator" which caused the explode function not to work. I apologize for this mistake.
k at skreel dot org
10-Jul-2003 01:36
str_split for older versions of PHP, it should behave just the same (let's hope that code indentation remains the same ^^).

 function str_split($chaine, $length=1)
 { $retour= FALSE;
   $incrmt= (int)$length;
   if (0 < $incrmt)
   { $retour= array();
     $offset= 0;
     $limite= strlen($chaine);
     while ($offset < $limite)
     { $retour[]= substr($chaine, $offset, $incrmt);
       $offset += $incrmt;
     }
   }// if (0 < $incrmt)
   return ($retour);
 }

<str_shufflestr_word_count>
 Last updated: Thu, 26 Feb 2004
show source | credits | sitemap | contact | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Sat Feb 28 05:26:30 2004 CET