PHP 8.3.4 Released!

explode

(PHP 4, PHP 5, PHP 7, PHP 8)

explodeBir dizgeyi bir ayraca göre bölüp bir dizi haline getirir

Açıklama

explode(string $ayraç, string $dizge, int $sınır = PHP_INT_MAX): array

Belirtilen dizge dizgesini belirtilen ayraç dizgesine göre alt dizgelere ayırıp bunları bir diziye yerleştirir.

Bağımsız Değişkenler

ayraç

Dizgeleri ayırmak için kullanılacak dizge.

dizge

Alt dizgelerine ayrılacak dizge.

sınır

sınır pozitifse dizi en çok sınır sayıda eleman içerir ve dizgenin kalanı son elemana yerleştirilir.

sınır negatifse, son sınır eleman hariç tüm elemanlar döndürülür.

sınır sıfırsa, 1 olarak ele alınır.

PHP 8.0.0 öncesinde, implode() işlevinin bağımsız değişkenleri yer değiştirebilirdi. Fakat explode() bunu asla desteklemez: ayraç bağımsız değişkeni dizge bağımsız değişkeninden önce belirtilmelidir.

Dönen Değerler

Belirtilen dizge dizgesini belirtilen ayraç dizgesine göre alt dizgelere ayırıp bunları yerleştirdiği diziyi döndürür.

ayraç boş ("") bir dizge ise explode() bir ValueError yavrular. Eğer ayraç dizgesi dizge içinde bulunmayan bir karakter içeriyorsa ve negatif bir sınır belirtilmişse boş bir dizi döner. Aksi takdirde, dizge dizgesini içeren bir dizi döner. Ayraçlar dizgenin başında veya sonunda görünürse, söz konusu değerler, döndürülen dizinin sırasıyla ilk veya son konumuna boş bir dizi değeri olarak eklenir.

Sürüm Bilgisi

Sürüm: Açıklama
8.0.0 ayraç olarak boş bir dize ("") belirtildiğinde explode() artık bir ValueError yavruluyor; evvelce false dönerdi.

Örnekler

Örnek 1 - explode() örneği

<?php
// 1. örnek
$pizza = "dilim1 dilim2 dilim3 dilim4 dilim5 dilim6";
$dilimler = explode(" ", $pizza);
echo
$dilimler[0]; // dilim1
echo $dilimler[1]; // dilim2

// 2. örnek
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list(
$user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo
$user; // foo
echo $pass; // *

?>

Örnek 2 explode() return examples

<?php
/*
A string that doesn't contain the delimiter will simply
return a one-length array of the original string.
*/
$input1 = "hello";
$input2 = "hello,there";
$input3 = ',';
var_dump( explode( ',', $input1 ) );
var_dump( explode( ',', $input2 ) );
var_dump( explode( ',', $input3 ) );

?>

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

array(1)
(
    [0] => string(5) "hello"
)
array(2)
(
    [0] => string(5) "hello"
    [1] => string(5) "there"
)
array(2)
(
    [0] => string(0) ""
    [1] => string(0) ""
)

Örnek 3 - sınır bağımsız değişkeni örnekleri

<?php
$str
= 'one|two|three|four';

// positif sınır
print_r(explode('|', $str, 2));

// negatif sınır
print_r(explode('|', $str, -1));
?>

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

Array
(
    [0] => one
    [1] => two|three|four
)
Array
(
    [0] => one
    [1] => two
    [2] => three
)

Notlar

Bilginize: Bu işlev ikil dosyalarla çalışırken dosya içeriğini değiştirmez.

Ayrıca Bakınız

  • preg_split() - Dizgeyi düzenli ifadeye göre böler
  • str_split() - Bir dizgeyi bir diziye dönüştürür
  • mb_split() - Çok baytlı bir dizgeyi düzenli ifade ile parçalara ayırır
  • str_word_count() - Bir dizgedeki sözcükler hakkında bilgi verir
  • strtok() - Dizgeyi bir dizgeciğe göre böler
  • implode() - Dizi elemanlarını birleştirip bir dizge elde eder

add a note

User Contributed Notes 4 notes

up
26
Gerben
1 year ago
Note that an empty input string will still result in one element in the output array. This is something to remember when you are processing unknown input.

For example, maybe you are splitting part of a URI by forward slashes (like "articles/42/show" => ["articles", "42", "show"]). And maybe you expect that an empty URI will result in an empty array ("" => []). Instead, it will contain one element, with an empty string:

<?php

$uri
= '';
$parts = explode('/', $uri);
var_dump($parts);

?>

Will output:

array(1) {
[0]=>
string(0) ""
}

And not:

array(0) {
}
up
3
marc
4 months ago
If your data is smaller than the expected count with the list expansion:

<?php
$data
= "foo:*:1023:1000::/home/foo:/bin/sh";
list(
$user, $pass, $uid, $gid, $gecos, $home, $shell,$nu) = explode(":", $data);
?>

The result is a warning not an error:

PHP Warning: Undefined array key 7 in ...

The solution is to pad the array to the expected length:

<?php
$data
= "foo:*:1023:1000::/home/foo:/bin/sh";
list(
$user, $pass, $uid, $gid, $gecos, $home, $shell,$nu) = array_pad( explode(":", $data), 8, "");
// where 8 is the count of the list arguments
?>
up
13
bocoroth
3 years ago
Be careful, while most non-alphanumeric data types as input strings return an array with an empty string when used with a valid separator, true returns an array with the string "1"!

var_dump(explode(',', null)); //array(1) { [0]=> string(0) "" }
var_dump(explode(',', false)); //array(1) { [0]=> string(0) "" }

var_dump(explode(',', true)); //array(1) { [0]=> string(1) "1" }
up
-1
Alejandro-Ihuit
1 year ago
If you want to directly take a specific value without having to store it in another variable, you can implement the following:

$status = 'Missing-1';

echo $status_only = explode('-', $status)[0];

// Missing
To Top