note 109403 modified in function.explode by googleguy

From: Date: Sat, 24 Nov 2012 20:40:50 +0000
Subject: note 109403 modified in function.explode by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192134@lists.php.net to get a copy of this message
Explode does not parse a string by delimiters, in the sense that we expect to find tokens between a starting and ending delimiter, but instead splits a string into parts by using a string as the boundary of each part. Once that boundary is discovered the string is split. Whether or not that boundary is proceeded or superseded by any data is irrelevant since the parts are determined at the point a boundary is discovered. For example: <?php var_dump(explode("/","/")); /* Outputs array(2) { [0]=> string(0) "" [1]=> string(0) "" } */ ?> The reason we have two empty strings here is that a boundary is discovered before any data has been collected from the string. The boundary splits the string into two parts even though those parts are empty. One way to avoid getting back empty parts (if you don't care for those empty parts) is to use array_filter on the result. <?php var_dump(array_filter(explode("/","/"))); /* Outputs array(0) { } */ ?> *[This note was edited by googleguy at php dot net for clarity]* --was-- Explode does not parse a string by delimiters, in the sense that we expect to find tokens between a starting and ending delimiter, but instead splits a string into parts by using a string as the boundary of each part. Once that boundary is discovered the string is split. Whether or not that boundary is proceeded or superseded by any data is irrelevant since the parts are determined at the point a boundary is discovered. For example: <?php var_dump(explode(\"/\",\"/\")); /* Outputs array(2) { [0]=> string(0) \"\" [1]=> string(0) \"\" } */ ?> The reason we have two empty strings here is that a boundary is discovered before any data has been collected from the string. The boundary splits the string into two parts even though those parts are empty. One way to avoid getting back empty parts (if you don\'t care for those empty parts) is to use array_filter on the result. <?php var_dump(array_filter(explode(\"/\",\"/\"))); /* Outputs array(0) { } */ ?> *[This note was edited by googleguy at php dot net for clarity]* http://php.net/manual/en/function.explode.php

« previous php.notes (#192134) next »