user1592380
user1592380

Reputation: 36217

How can I explode a string by more than one space, but not by exactly one space in php

I have a text line that is delimited by tabs and multiple spaces but not single spaces. I have found:

parsed = preg_split('/ +/', $line); // one or more spaces  

which can a split a line with one or more spaces. Is there a regex for only more than 1 space but not exactly 1 space?

Upvotes: 2

Views: 2386

Answers (2)

Guntram Blohm
Guntram Blohm

Reputation: 9819

Just use preg_split('/ +/', $line); -- this is a blank followed by one or more blanks. Note that there are 2 blanks between the / and the +, even if it looks like one. Or, you could write it as '/ {2,}/', which also means previous expression (the blank) repeated at least 2 times.

Upvotes: 4

jeffjenx
jeffjenx

Reputation: 17457

parsed = preg_split('/ {2,}/', $line); // two or more spaces

Upvotes: 2

Related Questions