-->

Using Preg_Split With Multiple Spaces

2019-07-24 10:12发布

问题:

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

回答1:

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



回答2:

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

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



回答3:

This is an old post but I figure I will add for completeness.

$arr = preg_split('/[\s][\s]{1,3}/', $string,-1,PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);



This has a lot more options to work with. Remember inside the brackets you customize the exact number of spaces to look for...
/[\s][\s]{1,4}/
or simply one or more times after the first match /[\s][\s]+/ Also know that there are flags to set with this to make handling the output easier.

PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE

One makes it return no empty results and the other includes the matched content in the returned content.

There are others (http://php.net/preg-split) one that captures the offset but changes the output structure a little bit.
//////////////////// UPDATE ////////////////
I ended up havng to use
/(\w+\W+)/
//matches the words and takes it and the white spaces to return.

For some reason preg_split wouldn't replace the spaces.
Its stranger to me because it worked at one point in time, then it didn't after some edits. Went back to when it worked and it worked on my test page and my live one. Started added edits to it trouble shooting and bam, neither return the white space in the results. So this worked for me great and simple



回答4:

This should work:

preg_split('/  +/', $data)


回答5:

The simplest way to achieve this is by using the \s which denotes whitespace. To get it only to work when there are at least two (\s\s) you are best off using the curly brace notation to say 2 or more. By not specifying the second argument in the curly braces you're saying anything greater than or equal to 2.

preg_split('/\s{2,}/', $data);

To test this code out try the following

$data = 'hello  here  is  a  test! Hello World';

$p = preg_split('/\s{2,}/', $data);

die(var_dump($p));

Which outputs as follows:

array(5) {
  [0]=>
  string(5) "hello"
  [1]=>
  string(4) "here"
  [2]=>
  string(2) "is"
  [3]=>
  string(1) "a"
  [4]=>
  string(17) "test! Hello World"
}