note 120391 deleted from function.explode by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:49:20 +0000 |
| Subject: | note 120391 deleted from function.explode by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215330@lists.php.net to get a copy of this message | ||
Note Submitter: Manngo
----
Sometimes I want to create a MINIMUM length array. For example, if the delimiter is not present at
all, you might still want an empty additional item.
This can be done by adding the delimiter to the string as follows:
<?php
$data='a';
$array=explode(':',"$data:");
print_r($array);
?>
If you want an EXACT number of elements, you can combine this with array_slice:
<?php
$data='a:::';
$array=explode(':',"$data:");
$array=array_slice($array,0,2); // exactly 2 elements
print_r($array);
?>