Вопрос

This might seem trivial but it's hurting my head. Can someone explain the reason The http://php.net/manual/es/function.explode.php examples show (which I have trimmed).

$pizza  = "piece1 piece2 piece3"; // string
$pieces = explode(" ", $pizza);
// after a var dump
array(3) {  [0]=> string(6) "piece1" 
            [1]=> string(6) "piece2" 
            [2]=> string(6) "piece3" }

This works lovely, removes all the spaces and does a nice little array to work with, however when I use something similar shown below

$path = "/test-gallery/2/"; // string
$urlpieces = explode("/", $path);
// after a var dump
array(4) {  [0]=> string(0) "" 
            [1]=> string(12) "test-gallery" 
            [2]=> string(1) "2" 
            [3]=> string(0) "" }

I get the first and last with an empty string. Why does it not remove the first and the last array elements? I can always add another step and remove it but the explode should take it all out shouldnt it?

Thanks for the advice in advance.

Это было полезно?

Решение

I can always add another step and remove it but the explode should take it all out shouldnt it?

No. that is how explode works. Delimiter is at both end so "" string was found at both the ends after exploding

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top