3

How can I split a string by white-space no mater how long the white-space is?

For example, from the following string:

"the    quick brown   fox        jumps   over  the lazy   dog"

I would get an array of

['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'];
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
gramme.ninja
  • 1,341
  • 3
  • 11
  • 11
  • 1
    There's loads of identical questions on here. Did you try searching? – Asad Saeeduddin Apr 21 '14 at 22:17
  • @PlantTheIdea That doesn't deal with whitespace that is longer than one character. – Asad Saeeduddin Apr 21 '14 at 22:18
  • Use javascript's `trim` for more reliable results... – Jack Apr 21 '14 at 22:18
  • 1
    Saying 'use JavaScript' on a PHP question is useless. PHP is used for making back-ends, JavaScript is on the front-end. If I make an API call in Postman (or any other front-end that the user can interfere with) there is no JavaScript involved, but I still want the API to respond correctly. – Skrrp Mar 10 '22 at 13:01

2 Answers2

11

You can use Regular Expressions to do this easily:

$string = 'the quick     brown fox      jumps 



over the 

lazy dog';


$words = preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY);

print_r($words);

This produces this output:

Array
(
    [0] => the
    [1] => quick
    [2] => brown
    [3] => fox
    [4] => jumps
    [5] => over
    [6] => the
    [7] => lazy
    [8] => dog
)
Quixrick
  • 3,190
  • 1
  • 14
  • 17
  • what do the -1 and PREG_SPLIT_NO_EMPTY do? – gramme.ninja Apr 21 '14 at 22:41
  • @gramme.ninja Those are the limit and the flags. See the documentation of the function: http://www.php.net/manual/en/function.preg-split.php – vvanasten Apr 21 '14 at 22:44
  • 2
    The `-1` means no limit. If you specify a positive number there, then it will only make that many matches. Like if I set that to 3, it would return `[0] => the, [1] => quick, [2] => brown fox jumps over...`. `PREG_SPLIT_NO_EMPTY` means that it will not return empty strings. For instance, if you had `\s` instead of `\s+`, then it would break up all of the spaces as individual entities and return just a bunch of empty strings along with the text you wanted. `PREG_SPLIT_NO_EMPTY` just tells it to ignore empty items and just give us the stuff we want. – Quixrick Apr 21 '14 at 22:46
  • `PREG_SPLIT_NO_EMPTY` is needed for the case that ther are white spaces at the beginning or end of the string. `-1` is the default an just needed to be able to set the 4th parameter. – Den Apr 08 '19 at 13:04
2

With regex:

$str = "the      quick brown fox jumps over the lazy dog";
$a = preg_split("~\s+~",$str);
print_r($a);

Please note: I modified your string to include a lot of whitespace between the first two words, since this is what you want.

The output:

Array ( [0] => the [1] => quick [2] => brown [3] => fox [4] => jumps [5] => over [6] => the [7] => lazy [8] => dog ) 

How this works:

\s+ means one or more white space characters. It is the delimiter that splits the string. Do note that what PCRE means by "white space characters" is not just the character you obtain by pressing the space bar, but also tabs, vertical tabs, carriage returns and new lines. This should work perfectly for your purpose.

References

  1. For further reading, you may want to have a look at these preg_split examples.
  2. preg_split manual page
zx81
  • 41,100
  • 9
  • 89
  • 105