{"id":255,"date":"2014-12-11T01:54:38","date_gmt":"2014-12-10T23:54:38","guid":{"rendered":"http:\/\/icefront.info\/wp\/?p=255"},"modified":"2025-10-26T17:31:44","modified_gmt":"2025-10-26T17:31:44","slug":"explode-empty-string-returns-one-element-array","status":"publish","type":"post","link":"https:\/\/icefront.info\/index.php\/2014\/12\/11\/explode-empty-string-returns-one-element-array\/","title":{"rendered":"Explode empty string returns one-element array"},"content":{"rendered":"<p>Pretty annoying, exploding an empty string with &#8211; for example space &#8211; returns an array with one element, indexed with 0 and containing an empty string:<\/p>\n<pre>$string = \"\";\nexplode(\" \", $string);<\/pre>\n<p>returns<\/p>\n<pre>array(\n  [0] =&gt; ''\n)<\/pre>\n<p>Exploding an empty string should return an empty array. No matter what biased logic do we follow.<\/p>\n<p>A simple (but buggy) workaround is to remove the unnecessary empty elements by using array_filter(). This function removes all the array elements whose values evaluate to Boolean false:<\/p>\n<pre>array_filter(explode(\" \", $string));<\/pre>\n<p>Returns:<\/p>\n<pre>array(\n)<\/pre>\n<p>Unfortunately &#8220;0&#8221; evaluates to false as well, therefore calling<\/p>\n<pre>array_filter(explode(\" \", \"0 0\"));<\/pre>\n<p>Returns an empty array!<\/p>\n\n\n<p>An elegant (but not the fastest) solution is a feature-rich version of explode():<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public function explode2(string $by, string $string, array $options = []):array {\n    $includeEmpty = boolval($options['include_empty'] ?? false);\n    $trimChars = strval($options['trim_chars'] ?? \" \\n\\r\\t\\v\\x00\");\n    $result = [];\n    foreach (explode($by, $string) as $raw) {\n        $trimmed = trim($raw, $trimChars);\n        if ($includeEmpty || strlen($trimmed)) {\n            $result[] = $trimmed;\n        }\n    }\n    return $result;\n}<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pretty annoying, exploding an empty string with &#8211; for example space &#8211; returns an array with one element, indexed with 0 and containing an empty string: $string = &#8220;&#8221;; explode(&#8221; &#8220;, $string); returns array( [0] =&gt; &#8221; ) Exploding an empty string should return an empty array. No matter what biased logic do we follow. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[9,10,11,13],"class_list":["post-255","post","type-post","status-publish","format-standard","hentry","category-php","tag-empty-string","tag-explode","tag-one-element-array","tag-php-2"],"_links":{"self":[{"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/posts\/255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/comments?post=255"}],"version-history":[{"count":1,"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/posts\/255\/revisions"}],"predecessor-version":[{"id":275,"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/posts\/255\/revisions\/275"}],"wp:attachment":[{"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/media?parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/categories?post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/tags?post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}