PHP Velho Oeste 2024

str_split

(PHP 5, PHP 7)

str_split문자열을 배열로 변환

설명

array str_split ( string $string [, int $split_length ] )

문자열을 배열로 변환합니다.

인수

string

입력 문자열.

split_length

조각의 최대 길이.

반환값

선택적인 split_length 인수를 지정하면, 반환하는 배열은 split_length 길이의 원소로 나누어집니다. 지정하지 않으면 하나의 원소에 한 문자가 들어갑니다.

split_length가 1보다 작을 경우에는 FALSE를 반환합니다. split_length 길이가 string의 길이보다 클 경우, 문자열 전체를 첫번째 배열 원소로 반환합니다.

예제

Example #1 str_split() 용례

<?php

$str 
"Hello Friend";

$arr1 str_split($str);
$arr2 str_split($str3);

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
)

참고

  • chuck_split()
  • preg_split() - 정규 표현식에 따라 문자열을 나눔
  • explode() - 문자열을 문자열로 나눕니다
  • count_chars() - 문자열 안에 사용한 문자에 대한 정보를 반환
  • str_word_count() - 문자열에서 사용한 단어에 대한 정보를 반환
  • for

add a note add a note

User Contributed Notes 3 notes

up
0
Julian
1 year ago
The function str_split() is not 'aware' of words. Here is an adaptation of str_split() that is 'word-aware'.

<?php

$array
str_split_word_aware(
   
'In the beginning God created the heaven and the earth. And the earth was without form, and void; and darkness was upon the face of the deep.',
   
32
);

var_dump($array);

/**
  * This function is similar to str_split() but this function keeps words intact; it never splits through a word.
  *
  * @return array<int, string>
  */
function str_split_word_aware(string $string, int $maxLengthOfLine): array
{
    if (
$maxLengthOfLine <= 0) {
        throw new
RuntimeException(sprintf('The function %s() must have a max length of line at least greater than one', __FUNCTION__));
    }
   
   
$lines = [];
   
$words = explode(' ', $string);

   
$currentLine = '';
   
$lineAccumulator = '';
    foreach (
$words as $currentWord) {

       
$currentWordWithSpace = sprintf('%s ', $currentWord);
       
$lineAccumulator .= $currentWordWithSpace;
        if (
strlen($lineAccumulator) < $maxLengthOfLine) {
           
$currentLine = $lineAccumulator;
            continue;
        }

       
$lines[] = $currentLine;

       
// Overwrite the current line and accumulator with the current word
       
$currentLine = $currentWordWithSpace;
       
$lineAccumulator = $currentWordWithSpace;
    }

    if (
$currentLine !== '') {
       
$lines[] = $currentLine;
    }

    return
$lines;
}

?>

OUTPUT:

<?php

array(5) {
  [
0]=> string(29) "In the beginning God created "
 
[1]=> string(30) "the heaven and the earth. And "
 
[2]=> string(28) "the earth was without form, "
 
[3]=> string(27) "and void; and darkness was "
 
[4]=> string(27) "upon the face of the deep. "
}

?>
up
-7
lskatz at gmail dot com
15 years ago
A good use of str_split is reverse translating an amino acid sequence.

<?php
/* reverse translate an aa sequence using its dna counterpart */
function reverseTranslate($aaSeq,$ntSeq){
 
$nt=str_split($ntSeq,3);
 
$aa=str_split($aaSeq,1);
 
$gapChar=array('*','-');

 
$numAa=count($aa);
 
$ntIndex=0;
 
$newNtSeq="";
  for(
$i=0;$i<$numAa;$i++){
   
// if the aa is a gap, then just put on a gap character
   
if(in_array($aa[$i],$gapChar)){
     
$newNtSeq.='---';
    }
    else{
     
$newNtSeq.=$nt[$ntIndex];
     
$ntIndex++;
    }
  }
  return
$newNtSeq;
}
?>
up
-7
alex-glebe at mail dot ru
1 year ago
Empty string Does'n returns Empty array!

$arr0 = str_split("");
print_r($arr0);

Array
(
    [0] =>
)
To Top