PHP 8.3.4 Released!

explode

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

explodeSuddivide una stringa

Descrizione

explode(string $separator, string $string, int $limit = ?): array

Questa funzione restituisce una matrice di stringhe, ciascuna delle quali è una parte di string ottenuta dividendo la stringa originale utilizzando separator come separatore di stringa. Se si imposta limit la matrice restituita conterrà al massimo limit elementi di cui l'ultimo conterrà la parte restante di string.

Se il parametro separator è impostato ad una stringa vuota (""), la funzione explode() restituirà false. Se separator contiene caratteri non presenti in string, allora explode() restituirà una matrice contenente string.

Se il parametro limit è negativo, sono restituiti tutti i componenti tranne gli ultimi limit elementi. Questa caratteristica è stata inserita in PHP 5.1.0.

Sebbene implode() può, per ragioni storiche, accettare i parametri in entrambi gli ordini, explode() non può. Occorre accertarsi che il parametro separator sia antecedente al parametro string.

Nota:

Il parametro limit è stato aggiunto dalla versione 4.0.1

Example #1 Esempi di uso di explode()

<?php
// Esempio 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo
$pieces[0]; // piece1
echo $pieces[1]; // piece2

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

?>

Example #2 Esempi del parametro limit

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

// limite positivo
print_r(explode('|', $str, 2));

// negativo (dal PHP 5.1)
print_r(explode('|', $str, -1));
?>

Il precedente esempio visualizzerà:

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

Nota: Questa funzione è binary-safe (gestisce correttamente i file binari)

Vedere anche preg_split(), spliti(), split(), strtok() e implode().

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