|
|
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.
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){
$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
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"){ 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;
}
}
?>
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
function str_split($str, $nr) {
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
|
|