Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Preg_Split With Multiple Spaces

Tags:

php

preg-split

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

like image 842
sysdomatic Avatar asked Dec 01 '22 07:12

sysdomatic


2 Answers

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

like image 180
Yamiko Avatar answered Dec 15 '22 03:12

Yamiko


This will also work to split the data by Multiple spaces, Single Space and also by click on new tab.

preg_split('/\s+/', $data)

like image 27
Bandana Sahu Avatar answered Dec 15 '22 02:12

Bandana Sahu