How can I explode a string by one or more spaces or tabs?
Example:
A B C D
I want to make this an array.
How can I explode a string by one or more spaces or tabs?
Example:
A B C D
I want to make this an array.
instead of using explode, try preg_split: http://www.php.net/manual/en/function.preg-split.php
This works:
$string = 'A B C D';
$arr = preg_split('/[\s]+/', $string);
I think you want preg_split
:
$input = "A B C D";
$words = preg_split('/\s+/', $input);
var_dump($words);
@OP it doesn't matter, you can just split on a space with explode. Until you want to use those values, iterate over the exploded values and discard blanks.
$str = "A B C D";
$s = explode(" ",$str);
foreach ($s as $a=>$b){
if ( trim($b) ) {
print "using $b\n";
}
}