Explode string by one or more spaces or tabs


Question

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.

1
131
11/24/2009 9:17:22 PM

Accepted Answer

$parts = preg_split('/\s+/', $str);
295
11/24/2009 9:17:21 PM

To separate by tabs:

$comp = preg_split("/[\t]/", $var);

To separate by spaces/tabs/newlines:

$comp = preg_split('/\s+/', $var);

To seperate by spaces alone:

$comp = preg_split('/ +/', $var);


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon