<p>PHP's explode function returns an array of strings split on some provided substring. It will return empty strings when there are leading, trailing, or consecutive delimiters, like this:</p>
<pre><code>var_dump(explode('/', '1/2//3/'));
array(5) {
  [0]=&gt;
  string(1) &quot;1&quot;
  [1]=&gt;
  string(1) &quot;2&quot;
  [2]=&gt;
  string(0) &quot;&quot;
  [3]=&gt;
  string(1) &quot;3&quot;
  [4]=&gt;
  string(0) &quot;&quot;
}
</code></pre>
<p>Is there some different function or option or anything that would return everything <em>except</em> the empty strings?</p>
<pre><code>var_dump(different_explode('/', '1/2//3/'));
array(3) {
  [0]=&gt;
  string(1) &quot;1&quot;
  [1]=&gt;
  string(1) &quot;2&quot;
  [2]=&gt;
  string(1) &quot;3&quot;
}
</code></pre>

[{'answer_id': 64606, 'author': 'James Aylett', 'author_id': 6302, 'author_profile': 'https://Stackoverflow.com/users/6302', 'pm_score': 2, 'selected': False, 'text': '<pre><code>function not_empty_string($s) {\n  return $s !== "";\n}\n\narray_filter(explode(\'/\', \'1/2//3/\'), \'not_empty_string\');\n</code></pre>\n'}, {'answer_id': 64608, 'author': 'ceejayoz', 'author_id': 1902010, 'author_profile': 'https://Stackoverflow.com/users/1902010', 'pm_score': 7, 'selected': True, 'text': '<p>Try <a href="http://php.net/preg_split" rel="nofollow noreferrer">preg_split</a>.</p>\n<p><code>$exploded = preg_split(\'@/@\', \'1/2//3/\', -1, PREG_SPLIT_NO_EMPTY);</code></p>\n'}, {'answer_id': 64619, 'author': 'Dave Gregory', 'author_id': 5677, 'author_profile': 'https://Stackoverflow.com/users/5677', 'pm_score': 5, 'selected': False, 'text': '<p>array_filter will remove the blank fields, here is an example without the filter:</p>\n\n<pre><code>print_r(explode(\'/\', \'1/2//3/\'))\n</code></pre>\n\n<p>prints:</p>\n\n<pre><code>Array\n(\n    [0] =&gt; 1\n    [1] =&gt; 2\n    [2] =&gt;\n    [3] =&gt; 3\n    [4] =&gt;\n)\n</code></pre>\n\n<p>With the filter:</p>\n\n<pre><code>php&gt; print_r(array_filter(explode(\'/\', \'1/2//3/\')))\n</code></pre>\n\n<p>Prints:</p>\n\n<pre><code>Array\n(\n    [0] =&gt; 1\n    [1] =&gt; 2\n    [3] =&gt; 3\n)\n</code></pre>\n\n<p>You\'ll get all values that resolve to "false" filtered out.  </p>\n\n<p>see  <a href="http://uk.php.net/manual/en/function.array-filter.php" rel="noreferrer">http://uk.php.net/manual/en/function.array-filter.php</a></p>\n'}, {'answer_id': 64623, 'author': 'Fire Lancer', 'author_id': 6266, 'author_profile': 'https://Stackoverflow.com/users/6266', 'pm_score': 0, 'selected': False, 'text': '<p>Write a wrapper function to strip them</p>\n\n<pre><code>function MyExplode($sep, $str)\n{\n    $arr = explode($sep, $str);\n    foreach($arr as $item)\n        if(item != "")\n            $out[] = $item;\n    return $out;\n}\n</code></pre>\n'}, {'answer_id': 64629, 'author': 'Bobby Jack', 'author_id': 5058, 'author_profile': 'https://Stackoverflow.com/users/5058', 'pm_score': -1, 'selected': False, 'text': '<p>I usually wrap it in a call to <a href="http://uk.php.net/manual/en/function.array-filter.php" rel="nofollow noreferrer">array_filter</a>, e.g.</p>\n\n<pre><code>var_dump(array_filter(explode(\'/\', \'1/2//3/\'))\n=&gt;\narray(3) {\n  [0]=&gt;\n  string(1) "1"\n  [1]=&gt;\n  string(1) "2"\n  [3]=&gt;\n  string(1) "3"\n}\n</code></pre>\n\n<p>Be aware, of course, that array keys are maintained; if you <em>don\'t</em> want this behaviour, remember to add an outer wrapper call to array_values().</p>\n'}, {'answer_id': 64658, 'author': 'Bullines', 'author_id': 27870, 'author_profile': 'https://Stackoverflow.com/users/27870', 'pm_score': -1, 'selected': False, 'text': '<p>PHP\'s <a href="http://us2.php.net/manual/en/function.split.php" rel="nofollow noreferrer">split function</a> is similar to the explode function, except that it allows you to enter a regex pattern as the delimiter.  Something to the effect of:</p>\n\n<pre><code>$exploded_arr = split(\'/\\/+/\', \'1/2//3/\');\n</code></pre>\n'}, {'answer_id': 64728, 'author': 'AntonioCS', 'author_id': 8715, 'author_profile': 'https://Stackoverflow.com/users/8715', 'pm_score': 0, 'selected': False, 'text': '<p>Use this function to filter the output of the explode function</p>\n\n<pre><code>  function filter_empty(&amp;$arrayvar) {\n        $newarray = array();\n        foreach ($arrayvar as $k =&gt; $value)\n            if ($value !== "")\n                $newarray[$k] = $value;\n\n        $arrayvar = $newarray;\n    }\n</code></pre>\n'}, {'answer_id': 64821, 'author': 'Glenn Moss', 'author_id': 5726, 'author_profile': 'https://Stackoverflow.com/users/5726', 'pm_score': 3, 'selected': False, 'text': "<p>Just for variety:</p>\n\n<pre><code>array_diff(explode('/', '1/2//3/'), array(''))\n</code></pre>\n\n<p>This also works, but does mess up the array indexes unlike preg_split. Some people might like it better than having to declare a callback function to use array_filter.</p>\n"}, {'answer_id': 72886, 'author': 'Adam Hopkinson', 'author_id': 12280, 'author_profile': 'https://Stackoverflow.com/users/12280', 'pm_score': 0, 'selected': False, 'text': "<p>Regular expression solutions tend to be much slower than basic text replacement, so i'd replace double seperators with single seperators, trim the string of any whitespace and then use explode:</p>\n\n<pre><code>// assuming $source = '1/2//3/';\n$source = str_replace('//', '/', $source);\n$source = trim($source);\n$parts = explode('/', $source);\n</code></pre>\n"}, {'answer_id': 23419614, 'author': 'Memochipan', 'author_id': 826500, 'author_profile': 'https://Stackoverflow.com/users/826500', 'pm_score': 1, 'selected': False, 'text': '<p>I have used this in <a href="http://doc-typo3.ameos.com/4.1.0/incfile_8php-source.html#l00011" rel="nofollow">TYPO3</a>, look at the <code>$onlyNonEmptyValues</code> parameter:</p>\n\n<pre><code>function trimExplode($delim, $string, $onlyNonEmptyValues=0){\n    $temp = explode($delim,$string);\n    $newtemp=array();\n    while(list($key,$val)=each($temp))      {\n        if (!$onlyNonEmptyValues || strcmp("",trim($val)))      {\n            $newtemp[]=trim($val);\n        }\n    }\n    reset($newtemp);\n    return $newtemp;\n}\n</code></pre>\n\n<p>It doesn\'t mess up the indexes:</p>\n\n<pre><code>var_dump(trimExplode(\'/\', \'1/2//3/\',1));\n</code></pre>\n\n<p>Result:</p>\n\n<pre><code>array(3) {\n  [0]=&gt;\n  string(1) "1"\n  [1]=&gt;\n  string(1) "2"\n  [2]=&gt;\n  string(1) "3"\n}\n</code></pre>\n'}, {'answer_id': 35928378, 'author': 'That Realty Programmer Guy', 'author_id': 578023, 'author_profile': 'https://Stackoverflow.com/users/578023', 'pm_score': 1, 'selected': False, 'text': "<p>Here is a solution that should output a newly indexed array.</p>\n\n<pre><code>$result = array_deflate( explode( $delim, $array) );\n\nfunction array_deflate( $arr, $emptyval='' ){\n    $ret=[];\n    for($i=0,$L=count($arr); $i&lt;$L; ++$i)\n        if($arr[$i] !== $emptyval) $ret[]=$arr[$i];\n    return $ret;\n}\n</code></pre>\n\n<p>While fairly similar to some other suggestion, this implementation has the benefit of generic use. For arrays with non-string elements, provide a typed empty value as the second argument. </p>\n\n<p><code>array_deflate( $objArray, new stdClass() );</code></p>\n\n<p><code>array_deflate( $databaseArray, NULL );</code></p>\n\n<p><code>array_deflate( $intArray, NULL );</code></p>\n\n<p><code>array_deflate( $arrayArray, [] );</code></p>\n\n<p><code>array_deflate( $assocArrayArray, [''=&gt;NULL] );</code></p>\n\n<p><code>array_deflate( $processedArray, new Exception('processing error') );</code></p>\n\n<p>.</p>\n\n<p>.</p>\n\n<p>.</p>\n\n<p>With an optional filter argument..</p>\n\n<pre><code>function array_deflate( $arr, $trigger='', $filter=NULL, $compare=NULL){\n    $ret=[];\n    if ($filter === NULL) $filter = function($el) { return $el; };\n    if ($compare === NULL) $compare = function($a,$b) { return $a===$b; };\n\n    for($i=0,$L=count($arr); $i&lt;$L; ++$i)\n        if( !$compare(arr[$i],$trigger) ) $ret[]=$arr[$i];\n        else $filter($arr[$i]);\n    return $ret;\n}\n</code></pre>\n\n<p>With usage..</p>\n\n<pre><code>function targetHandler($t){ /* .... */ }    \narray_deflate( $haystack, $needle, targetHandler );\n</code></pre>\n\n<p>Turning array_deflate into a way of processing choice elements and removing them from your array. Also nicer is to turn the if statement into a comparison function that is also passed as an argument in case you get fancy.</p>\n\n<p><code>array_inflate</code> being the reverse, would take an extra array as the first parameter which matches are pushed to while non-matches are filtered.</p>\n\n<pre><code>function array_inflate($dest,$src,$trigger='', $filter=NULL, $compare=NULL){\n    if ($filter === NULL) $filter = function($el) { return $el; };\n    if ($compare === NULL) $compare = function($a,$b) { return $a===$b; };\n\n    for($i=0,$L=count($src); $i&lt;$L; ++$i)\n        if( $compare(src[$i],$trigger) ) $dest[]=$src[$i];\n        else $filter($src[$i]);\n    return $dest;\n}\n</code></pre>\n\n<p>With usage..</p>\n\n<pre><code>$smartppl=[];    \n$smartppl=array_inflate( $smartppl,\n                         $allppl,\n                         (object)['intelligence'=&gt;110],\n                         cureStupid,\n                         isSmart);\n\nfunction isSmart($a,$threshold){\n    if( isset($a-&gt;intellgence) )    //has intelligence?\n        if( isset($threshold-&gt;intellgence) )    //has intelligence?\n            if( $a-&gt;intelligence &gt;= $threshold-&gt;intelligence )\n                return true;\n            else return INVALID_THRESHOLD; //error\n        else return INVALID_TARGET; //error\n    return false;\n}\n\nfunction cureStupid($person){\n    $dangerous_chemical = selectNeurosteroid();\n    applyNeurosteroid($person, $dangerous_chemical);\n\n    if( isSmart($person,(object)['intelligence'=&gt;110]) ) \n        return $person;\n    else \n        lobotomize($person);\n\n    return $person;\n}\n</code></pre>\n\n<p>Thus providing an ideal algorithm for the world's educational problems. Aaand I'll stop there before I tweak this into something else..</p>\n"}, {'answer_id': 36192965, 'author': 'Jeff', 'author_id': 6107585, 'author_profile': 'https://Stackoverflow.com/users/6107585', 'pm_score': 0, 'selected': False, 'text': '<p>No regex overhead - should be reasonably efficient, strlen just counts the bytes</p>\n\n<p>Drop the array_values() if you don\'t care about indexes</p>\n\n<p>Make it into function explode_interesting( $array, $fix_index = 0 ) if you want</p>\n\n<pre><code>$interesting = array_values( \n                 array_filter(\n                   explode(\'/\', \'/1//2//3///4/0/false\' ),\n                   function ($val) { return strlen($val); }\n               ));\n\necho "&lt;pre&gt;", var_export( $interesting, true ), "&lt;/pre&gt;";\n</code></pre>\n\n<p>enjoy, Jeff</p>\n'}]
['https://Stackoverflow.com/questions/64570', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5726/']