PHP 8.3.4 Released!

str_split

(PHP 5, PHP 7, PHP 8)

str_splitBir dizgeyi bir diziye dönüştürür

Açıklama

str_split(string $dizge, int $uzunluk = 1): array

Bir dizgeyi bir diziye dönüştürür.

Bağımsız Değişkenler

dizge

Girdi dizgesi.

uzunluk

Azami parça uzunluğu.

Dönen Değerler

Seçimlik uzunluk bağımsız değişkeni belirtilmişse dönen dizi, dizge eşit olarak bölünmezse daha kısa olabilecek son parça hariç uzunluk baytlık/karakterlik parçalar içerir. uzunluk öntanımlı olarak 1'dir, yani her parça tek bayttır.

uzunluk 1'den küçükse işlev false döndürür. Eğer uzunluk, dizge uzunluğundan büyükse dizinin tek elemanında dizgenin tamamı bulunur.

Hatalar/İstisnalar

uzunluk 1'den küçükse ValueError yavrulanır.

Sürüm Bilgisi

Sürüm: Açıklama
8.2.0 dizge boşsa boş bir dizi döner. Evvelce boş dizge içeren bir dizi dönerdi.
8.0.0 uzunluk 1'den küçükse artık ValueError yavrulanıyor, evvelce E_WARNING seviyesinde bir hata oluşurdu ve işlev false döndürürdü.

Örnekler

Örnek 1 - str_split() örneği

<?php

$str
= "Hey Arkadaşım";

$arr1 = str_split($str);
$arr2 = str_split($str, 4);

print_r($arr1);
print_r($arr2);

?>

Yukarıdaki örneğin çıktısı:

Array
(
    [0] => H
    [1] => e
    [2] => y
    [3] =>
    [4] => A
    [5] => r
    [6] => k
    [7] => a
    [8] => d
    [9] => a
    [10] => �
    [11] => �
)
Array
(
    [0] => Hey
    [1] => Arka
    [2] => daş
    [3] => ım
)

Notlar

Bilginize:

str_split() çok baytlı kodlanmış bir dizgeyle uğraşırken karakterler yerine baytları ayıklar. Karakterlerin ayıklanması için bunun yerine mb_str_split() kullanılmalıdır.

Ayrıca Bakınız

  • mb_str_split() - Belirtilen çok baytlı dizgenin karakterlerini içeren bir dizi döndürür
  • chunk_split() - Bir dizgeyi daha küçük parçalara ayırır
  • preg_split() - Dizgeyi düzenli ifadeye göre böler
  • explode() - Bir dizgeyi bir ayraca göre bölüp bir dizi haline getirir
  • count_chars() - Bir dizgedeki karakterler hakkında bilgi döndürür
  • str_word_count() - Bir dizgedeki sözcükler hakkında bilgi verir
  • 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