[php]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
)
其它範例
function str_rsplit($str, $sz)
{
// splits a string "starting" at the end, so any left over (small chunk) is at the beginning of the array.
if ( !$sz ) { return false; }
if ( $sz > 0 ) { return str_split($str,$sz); } // normal split
$l = strlen($str);
$sz = min(-$sz,$l);
$mod = $l % $sz;
if ( !$mod ) { return str_split($str,$sz); } // even/max-length split
// split
return array_merge(array(substr($str,0,$mod)), str_split(substr($str,$mod),$sz));
}
$str = 'aAbBcCdDeEfFg';
str_split($str,5); // return: {'aAbBc','CdDeE','fFg'}
str_rsplit($str,5); // return: {'aAbBc','CdDeE','fFg'}
str_rsplit($str,-5); // return: {'aAb','BcCdD','eEfFg'}
//給php4使用的範例
function str_split_php4( $text, $split = 1 ) {
// place each character of the string into and array
$array = array();
for ( $i=0; $i < strlen( $text ); ){
$key = NULL;
for ( $j = 0; $j < $split; $j++, $i++ ) {
$key .= $text[$i];
}
array_push( $array, $key );
}
return $array;
}
公告版位
礁溪溫泉套房,位於宜蘭縣礁溪國小旁,近麥當勞,離礁溪火車站、首都客運、葛瑪蘭汽車客運約5分鍾車程,礁溪溫泉套房出租,您不需要再行添購傢俱,只需要帶幾件衣服,就能輕輕鬆鬆住進礁溪溫泉套房,天天在礁溪溫泉套房洗溫泉唷!意洽:游媽媽,電話0939711360,4500~4900元/月
- Jun 29 Wed 2011 09:41
[php]str_split — 將字符串轉換為數組
close
全站熱搜
留言列表