글자 쪼개서 배열에 넣기 정보
JavaScript 글자 쪼개서 배열에 넣기본문
word_split
문자를 $cnt 개수만큼 잘라 배열에 담는다
-------------------------------------------------------------
$str = 문자열
$cnt = 자를 개수
$mode = true = $cnt 개수만큼 계속 추가 함, false = 사용않함
-------------------------------------------------------------
예제 1
$str = "1111111";
$array = word_split($str, 2, false);
print_r($array);
결과 1
Array
[0] = 11
[1] = 11
[2] = 11
[3] = 1
예제 2
$str = "1111111";
$array = word_split($str, 2, true);
print_r($array);
결과 2
Array
[0] = 11
[1] = 1111
[2] = 111111
[3] = 1111111
-------------------------------------------------------------
function word_split($str, $cnt, $mode="")
{
$cnt = (int)$cnt;
$strlen = strlen($str);
$total_str = $strlen / $cnt;
$arr = array();
if ( $mode )
{
$mode_array = array("true", "false");
if ( ! strlen( array_search( $mode, $mode_array ) ) )
return die( " mode 는 true 나 false 둘중 하나만 입력하세요" );
}
else
{
$mode = false;
}
for ( $i = 0; $i < $total_str; $i++ )
{
if ( $mode == '' ) $array[$i] = substr( $str, $i * $cnt, $cnt );
elseif ( $mode ) $array[$i] = substr( $str, 0, ( $i * $cnt ) + $cnt );
}
return $array;
}
1
댓글 0개