2

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?

user1592380
  • 34,265
  • 92
  • 284
  • 515

2 Answers2

4

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.

Guntram Blohm
  • 9,667
  • 2
  • 24
  • 31
2
parsed = preg_split('/ {2,}/', $line); // two or more spaces
jeffjenx
  • 17,041
  • 6
  • 57
  • 99
  • `\s` matches any whitespace character (tabs, spaces, newlines). The OP requested to match multiple spaces. – jeffjenx Dec 10 '13 at 18:25
  • 1
    Warning - \s can be a new line, carriage return, horizontal tab, vertical tab or just anything "space-like" as well. – Guntram Blohm Dec 10 '13 at 18:25
  • Yikes! My mistake, sorry. I can't seem to re-edit my original comment, but I suggested that MrSlayer use the regex `'/\s{2,}/'` for readability – Rob M. Dec 10 '13 at 18:27
  • No need to be sorry, @RobM. Better to leave your comment for future visitors who might find that type of stuff useful. – jeffjenx Dec 10 '13 at 18:29