4

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

sysdomatic
  • 41
  • 1
  • 1
  • 2

5 Answers5

9

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

Community
  • 1
  • 1
Yamiko
  • 5,303
  • 5
  • 30
  • 52
  • 1
    Yes, I actually think he meant one space, not whitespace. Btw, it should be \s\s+ if you'd like to use that solution. – deviousdodo Nov 01 '11 at 01:37
8

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

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

Bandana Sahu
  • 284
  • 2
  • 10
  • This is 100% incorrect because the never-edited question says `I am looking to blow up this line where there is more than one space, but not just one space`. This pattern explodes on one or more spaces, so `Alpha Natural Resources Inc` will not "stay together". – mickmackusa Jul 09 '22 at 12:59
2

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

JSG
  • 390
  • 1
  • 4
  • 13
  • 1
    There is no benefit to the square braces in the pattern. Character classes with a single entry are pointless. Just write the character on its own. – mickmackusa Mar 12 '22 at 11:04
  • mickmackusa - Yes, I believe that is an alternative way to use less typing. However when I tested this it worked as I stated and it is easier to understand for me personally. I came to the conclusion to use this structure because it shows up in the examples I found. So I went with it in hopes I may make it easier to understand for a majority of others as well. – JSG Jun 10 '22 at 16:26
0

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"
}
Simon R
  • 3,732
  • 4
  • 31
  • 39
0

This should work:

preg_split('/  +/', $data)
deviousdodo
  • 9,177
  • 2
  • 29
  • 34