tags:

views:

257

answers:

5

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.

+8  A: 
$parts = preg_split('/\s+/', $str);
Ben James
+1  A: 

instead of using explode, try preg_split: http://www.php.net/manual/en/function.preg-split.php

Brian Schroth
+2  A: 

This works:

$string = 'A   B C          D';
$arr = preg_split('/[\s]+/', $string);
schneck
+1  A: 

I think you want preg_split:

$input = "A  B C   D";
$words = preg_split('/\s+/', $input);
var_dump($words);
jheddings
A: 

@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";
    }
}
ghostdog74