Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

str_split

(PHP 5)

str_split --  Преобразует строку в массив

Описание

array str_split ( string string [, int split_length] )

Преобразует строку в массив. Если указан необязательный аргумент split_length, возвращаемый массив будет содержать части исходной строки длиной split_length каждая, иначе каждый элемент будет содержать один символ.

Если split_length меньше 1, возвращается FALSE. Если split_length больше длины строки string, вся строка будет возвращена в первом и единственном элементе массива.

Пример 1. Примерв использования str_split()

<?php

$str
= "Hello Friend";

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

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

?>

Вывод:

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
)

См. также описание функций chunk_split(), preg_split(), split(), count_chars() и str_word_count(), а также for.



str_word_count> <str_shuffle
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
str_split
dacmeaux at gmail dot com
01-Nov-2007 02:57
I was looking for a function that would split a string into an array like str_split() and found Razor's function above. Just though that I would simplify the code a little.

<?php
function str_split_php4($text, $split = 1){
   
//place each character of the string into and array
   
$array = array();
    for(
$i=0; $i < strlen($text); $i++){
       
$key = NULL;
        for (
$j = 0; $j < $split; $j++){
           
$key .= $text[$i]; 
        }
       
array_push($array, $key);
    }
    return
$array;
}
?>

Both mine and worksRazor's work well, I just prefer to use less code. I could have written one myself, but I was just being lazy.
Sam
25-Sep-2007 01:24
A good way to use this method to convert CamelCase text into nice text would be-

<?php
       
/**
         Returns a formatted string based on camel case.
         e.g. "CamelCase" -> "Camel Case".
        */
       
function FormatCamelCase( $string ) {
               
$output = "";
                foreach(
str_split( $string ) as $char ) {
                       
strtoupper( $char ) == $char and $output and $output .= " ";
                       
$output .= $char;
                }
                return
$output;
        }
?>
edgaras dot janusauskas at gmail dot com
22-Aug-2007 05:30
To split unicode text, use preg_split('//u', $text);
kjensen at iaff106 dot com
11-Aug-2007 12:59
Here is what I use. I started with examples here but modified to my own version:

<?php
if (phpversion () < "5"){ // define PHP5 functions if server uses PHP4

function str_split($text, $split = 1)
{
if (!
is_string($text)) return false;
if (!
is_numeric($split) && $split < 1) return false;
$len = strlen($text);
$array = array();
$s = 0;
$e=$split;
while (
$s <$len)
    {
       
$e=($e <$len)?$e:$len;
       
$array[] = substr($text, $s,$e);
       
$s = $s+$e;
    }
return
$array;
}
}
?>
Siemen Paulsen
11-Jul-2007 12:46
Meercat9--

Look at the str_split() function.
l0c4lh0st DOT nl AT gmail DOT com
12-Jun-2007 05:28
how I can conwert
$string
 '1, 2, 5, 6, 10, 13, 23'
from ENUM at mySQL to

$array
[0] -> false
[1] -> true
[2] -> true
[3] -> false
[4] -> false
[5] -> true
[6] -> true
[7] -> false
[8] -> false
[9] -> false
[10] -> true
[11] -> false
[12] -> false
[13] -> true
[14] -> false
[15] -> false
...
[23] -> true

<?php
function enum_to_array($psEnum)
{
   
$aReturn = array();
   
$aTemp = explode(', ', $psEnum);
    for (
$i = $aTemp[0]; $i <= $aTemp[count($aTemp)-1]; $i++)
    {
       
$aReturn[$i] = in_array($i, $aTemp);
    }
}
?>
dhayes
07-Jun-2007 12:17
@razor: this'll work for php4
<?php
$str
= 'two words';
$array = explode("\r\n", chunk_split($str,1));
?>
M@rek (from Poland)
27-May-2007 07:25
how I can conwert
$string
 '1, 2, 5, 6, 10, 13, 23'
from ENUM at mySQL to

$array
[0] -> false
[1] -> true
[2] -> true
[3] -> false
[4] -> false
[5] -> true
[6] -> true
[7] -> false
[8] -> false
[9] -> false
[10] -> true
[11] -> false
[12] -> false
[13] -> true
[14] -> false
[15] -> false
...
[23] -> true
Razor
10-May-2007 12:02
heres my version for php4 and below

<?php

function str_split_php4($text, $split = 1)
{
    if (!
is_string($text)) return false;
    if (!
is_numeric($split) && $split < 1) return false;
   
   
$len = strlen($text);
   
   
$array = array();
   
   
$i = 0;
   
    while (
$i < $len)
    {
       
$key = NULL;
       
        for (
$j = 0; $j < $split; $j += 1)
        {
           
$key .= $text{$i};
           
           
$i += 1;   
        }
       
       
$array[] = $key;
    }
   
    return
$array;
}

?>
26-Feb-2007 08:32
Problem with the post below me is, that the string can not contain the splitter "-1-".

Btw, here's my version.
<?php
function strsplit($str, $l=1) {
    do {
$ret[]=substr($str,0,$l); $str=substr($str,$l); }
    while(
$str != "");
    return
$ret;
}
?>
webmaster at nsssa dot ca
28-Oct-2006 07:45
I noticed in the post below me that his function would return an array with an empty key at the end.

So here is just a little fix for it.

<?php

//Create a string split function for pre PHP5 versions
function str_split($str, $nr) {  
            
    
//Return an array with 1 less item then the one we have
    
return array_slice(split("-l-", chunk_split($str, $nr, '-l-')), 0, -1);
     
}

?>
05-Oct-2006 05:14
//fast & short version od str_split PHP3, 4x
function string_split($str, $nr){    
    return split("-l-", chunk_split($str, $nr, '-l-'));
}
//example :
print_r(string_split('123412341234', 4));
ference at super_delete_brose dot co dot uk
31-Aug-2006 12:22
If you are looking for a way to split multibyte strings then this may come in handy:

<?php
$text 
= "s
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571