Using Preg_Split With Multiple Spaces

sysdomatic picture sysdomatic · Nov 1, 2011 · Viewed 16.2k times · Source

I'm having a bit of trouble figuring this out.

I have lines of data such as this:

$data = "Alpha Natural Resources Inc COM 02076X102 2,077 45,700 x

I am looking to "explode" this line wherever there is more than one space. The problem that I have run into is that I have only found solutions that blow up the line where there is one space or more - I am looking to blow up this line where there is more than one space, but not just one space (so that Alpha Natural Resources Inc stay together, for instance).

I know that the answer is found in preg_split, but I can't figure out the proper code..

Thanks

Answer

Yamiko picture Yamiko · Nov 1, 2011

preg_split('/\s\s+/', $data) this while match the multiple of any whitespace such as return, tab etc. preg_split('/ +/', $data) will match only whitespace from the spacebar. \s selects any white space characters. Remove multiple whitespaces