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.

Değiştirgeler

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 değiştirgeleri yer değiştirebilirdi. Fakat explode() bunu asla desteklemez: ayraç değiştirgesi dizge değiştirgesinden ö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() false döndürür. 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.

Ö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_dumpexplode','$input1 ) );
var_dumpexplode','$input2 ) );
var_dumpexplode','$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 değiştirgesi örnekleri

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

// positif sınır
print_r(explode('|'$str2));

// 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 add a note

User Contributed Notes 5 notes

up
6
Emilio Bravo
3 years ago
$string = "PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION";
$exploded = explode("::",$string);
/*

explode('::',$string) = eliminate every :: and for each division of ::make an array element

Example:

PDO::ERRMODE_EXCEPTION (exploded) = array     (
                                                    [0] => string PDO
                                                    [1] => string ERRMODE_EXCEPTION
                                               )
Example:

$exploded[0] = "PDO";
*/
foreach ($exploded as $index) {
    echo $index . "\n";
}
/*

Output:

PDO
ATTR_ERRMODE => PDO
ERRMODE_EXCEPTION

*/
up
2
henrik Schmidt
3 years ago
"Return value" text needs updating for php 8, an empty delimiter now throws an Exception.
up
3
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
-5
David Spector
3 years ago
When using 'explode' to create an array of strings from a user-specified string that contains newline characters, you may wish the resulting array to correctly reflect the user's intentions by ignoring any final empty line (many users like to end multi-line input with a final newline, for clarity).

Here is a function to call after 'explode' to support this effect:

// When using explode, delete the last line in the array of lines when it is empty
function IgnoreEmptyLastLine(&$linesArr)
    {
    $last=count($linesArr)-1;
    if ($last>=0 && !$linesArr[$last])
        unset($linesArr[$last]);
    } // IgnoreEmptyLastLine
up
-26
leandro at primersistemas dot com dot br
3 years ago
function aexplode($delimiters,$string,$trimduplicate = false) {
    if (!is_array($delimiters))
        return explode($delimiters,$string);
    $stringaux = str_replace($delimiters, $delimiters[0], $string);
    if ($trimduplicate)
        while (strpos($stringaux,$delimiters[0].$delimiters[0]) !== false)
            $stringaux = str_replace($delimiters[0].$delimiters[0],$delimiters[0],$stringaux);
    return explode($delimiters[0],$stringaux);
}

This functions will work and accept array.
To Top