|
 |
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_split
user at mockme dot com
25-Mar-2006 05:53
found this great example on a php board for those not using php5, as an alternative to the posts below this
<?php
if(!function_exists('str_split')){
function str_split($string,$split_length=1){
$count = strlen($string);
if($split_length < 1){
return false;
} elseif($split_length > $count){
return array($string);
} else {
$num = (int)ceil($count/$split_length);
$ret = array();
for($i=0;$i<$num;$i++){
$ret[] = substr($string,$i*$split_length,$split_length);
}
return $ret;
}
}
}
?>
simple
15-Mar-2006 07:54
if (!function_exists("str_split")) {
function str_split($string, $length = 1) {
if ($length <= 0) {
trigger_error(__FUNCTION__."(): The the length of each segment must be greater then zero:", E_USER_WARNING);
return false;
}
$splitted = array();
while (strlen($string) > 0) {
$splitted[] = substr($string, 0, $length);
$string = substr($string, $length);
}
return $splitted;
}
}
14-Mar-2006 05:49
Note to function by carlosreche at yahoo dot com.
The while:
<?php
...
while ($str_length--) {
$splitted[$i] = $string[$i++];
}
...
?>
.. result in index starting at 1.
Ie: str_split("ABC") gives
Array
(
[1] => A
[2] => B
[3] => C
)
While php5's str_split("ABC") gives
Array
(
[0] => A
[1] => B
[2] => C
)
And his str_split("ABC",2) gives index starting at 0.
Change to this (or something similar):
<?php
...
while ($str_length--) {
$splitted[$i] = $string[$i];
$i++;
}
...
?>
.... or use heavyraptor's function. A bit more sclick,..
heavyraptor
10-Mar-2006 02:07
I think that the last post by carlosreche at yahoo dot com is too complicated.
It's much easier if you do it like this:
<?php
if (!function_exists("str_split")) {
function str_split($str,$length = 1) {
if ($length < 1) return false;
$strlen = strlen($str);
$ret = array();
for ($i = 0; $i < $strlen; $i += $length) {
$ret[] = substr($str,$i,$length);
}
return $ret;
}
}
?>
I hope it helps for those with PHP <5
carlosreche at yahoo dot com
15-Feb-2006 04:23
For those who work with PHP < 5:
<?php
if (!function_exists("str_split")) {
function str_split($string, $length = 1) {
if ($length <= 0) {
trigger_error(__FUNCTION__."(): The the length of each segment must be greater then zero:", E_USER_WARNING);
return false;
}
$splitted = array();
$str_length = strlen($string);
$i = 0;
if ($length == 1) {
while ($str_length--) {
$splitted[$i] = $string[$i++];
}
} else {
$j = $i;
while ($str_length > 0) {
$splitted[$j++] = substr($string, $i, $length);
$str_length -= $length;
$i += $length;
}
}
return $splitted;
}
}
?>
Hage Yaapa
04-Feb-2006 06:27
The very handy str_split() was introduced in PHP 5, but a lot of us are still forced to use PHP 4 at our host servers. And I am sure a lot of beginners have looked or are looking for a function to accomplish what str_split() does.
Taking advantge of the fact that strings are 'arrays' I wrote this tiny but useful e-mail cloaker in PHP, which guarantees functionality even if JavaScript is disabled in the client's browser. Watch how I make up for the lack of str_split() in PHP 4.3.10.
<?php
function cloakEmail($email) {
$arChars = array();
for ($i = 0; $i < strlen($email); $i++) { $arChars[] = $email[$i]; }
foreach ($arChars as $char) { print '&#'.ord($char); }
}
print cloakEmail('someone@nokikon.com');
?>
###### THE CODE ABOVE WITHOUT COMMENTS ######
<?php
function cloakEmail($email) {
$arChars = array();
for ($i = 0; $i < strlen($email); $i++) { $arChars[] = $email[$i]; }
foreach ($arChars as $char) { print '&#'.ord($char); }
}
print cloakEmail('someone@nokikon.com');
?>
In creating this little utility, I demonstrated how the lack of str_split() can be made up in PHP < 5. If you got how it was accomplished, you could write a function to do exactly what str_split() does in PHP 5 and even name it 'str_split()'. :)
organek at hektor dot umcs dot lublin dot pl
21-May-2005 04:57
[Editor's Note: Or just: php.net/wordwrap]
This is a little function to split a string into shorter strings with max lenght $n in such way, that it don't split words (it search for spaces), it's usefull for articles or sth.
Result is put in $ttab variable, and function result is number of "pages".
<?php
function divide_text($text, $n, &$ttab) {
$ttab = array();
$l = strlen($text); $cb = 0; $p = 0; if ($l <= $n) {
$ttab[0] = $text;
return 1;
} else {
$ctrl = 1;
while(((($p-1) * $n) < $l) && ($ctrl < 100)) {
$crtl++; $tmp = substr($text, $cb, $n);
$lastpos = strrpos($tmp," ");
if ( (is_bool($lastbool) && !$lastpos) || ( $l - $cb <= $n)) {
$ttab[$p] = $tmp;
} else {
$tmpgood = trim(substr($tmp, 0,$lastpos)); $ttab[$p] = $tmpgood;
$cb += $lastpos + 1 ;
}; $p++;
}; return $p;
}; } ?>
| |