PHP 8.3.4 Released!

str_split

(PHP 5, PHP 7, PHP 8)

str_split文字列を配列に変換する

説明

str_split(string $string, int $length = 1): array

文字列を配列に変換します。

パラメータ

string

入力文字列。

length

分割した部分の最大長。

戻り値

オプションのパラメータ length が指定されている場合、 返される配列の各要素は、length の長さとなります。但し、平等に分割できない場合、 最後の要素だけはその長さより短くなります。 デフォルトでは、 length の値は 1 です。 つまり、 1バイトずつ分割された配列となります。

エラー / 例外

length が1より小さい場合、 ValueError がスローされます。

変更履歴

バージョン 説明
8.2.0 string が空の場合、 空の配列が返されるようになりました。 これより前のバージョンでは、空文字列がひとつ入った配列が返されていました。
8.0.0 length が1より小さい場合、 ValueError がスローされるようになりました。 それより前のバージョンでは、 E_WARNING レベルの警告が発生し、 false を返していました。

例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
)

注意

注意:

str_split() でのマルチバイト文字列の扱いは、文字単位ではなくバイト単位での分割になります。 コードポイント単位で文字列を分割するには、 mb_str_split() を使います。

参考

  • mb_str_split() - マルチバイト文字列を受取り、文字の配列を返す
  • chunk_split() - 文字列をより小さな部分に分割する
  • preg_split() - 正規表現で文字列を分割する
  • explode() - 文字列を文字列により分割する
  • count_chars() - 文字列で使用されている文字に関する情報を返す
  • str_word_count() - 文字列に使用されている単語についての情報を返す
  • for

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