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

2
  • A better way to have asked this question was to say that you'd like to find every instance of two spaces or more. Commented Nov 1, 2011 at 1:40
  • @Andre But why? "more than one" and "two or more" are exactly the same thing. Commented Aug 6, 2021 at 21:11

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I actually think he meant one space, not whitespace. Btw, it should be \s\s+ if you'd like to use that solution.
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)

1 Comment

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".
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

2 Comments

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 - 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.
0

This should work:

preg_split('/  +/', $data)

Comments

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"
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.