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