Explode string by one or more spaces or tabs
Asked Answered
M

8

165

How can I explode a string by one or more spaces or tabs?

Example:

A      B      C      D

I want to make this an array.

Mateusz answered 24/11, 2009 at 21:12 Comment(2)
zero or more spaces implies that either each element will have at most one character, or that you'll have infinitely many empty elements. Are you sure this is what you want?Scrapple
Yeah, that should probably be "one or more spaces".Colpotomy
D
382
$parts = preg_split('/\s+/', $str);
Deposal answered 24/11, 2009 at 21:17 Comment(1)
Instead of removing the last part which may be empty, one can use: $parts = preg_split('/\s+/', $str, -1, PREG_SPLIT_NO_EMPTY);Willard
K
65

To separate by tabs:

$comp = preg_split("/\t+/", $var);

To separate by spaces/tabs/newlines:

$comp = preg_split('/\s+/', $var);

To seperate by spaces alone:

$comp = preg_split('/ +/', $var);

Keikokeil answered 27/11, 2014 at 20:53 Comment(2)
To add to this preg_split('/\s{2,}/', $var); which requires atleast two spaces,Cerelly
There is no benefit to writing \t inside of a character class.Ephesus
C
26

This works:

$string = 'A   B C          D';
$arr = preg_split('/\s+/', $string);
Calvinna answered 24/11, 2009 at 21:17 Comment(3)
There is no benefit to writing \s inside of a character class.Ephesus
@Ephesus Could you clarify? This is pretty similar to the first example in the docs.Evie
@Hash the documentation's example is splitting on whitespaces AND commas. When you have multiple items to list ([\s,]), then creating a "character class" is appropriate. When you are only splitting on \s (whitespaces), then use the current syntax in this answer (edited by Dharman after I commented).Ephesus
E
19

The author asked for explode, to you can use explode like this

$resultArray = explode("\t", $inputString);

Note: you must used double quote, not single.

Emendation answered 20/7, 2016 at 12:28 Comment(5)
Worked for me and is a tad simpler than using the dark power of regular expressions.Circinate
But he asked for "spaces or tabs" and this only explodes for tabs.Newmint
I came here looking for exploding spaces too. I feel sad deep inside about this.Cooler
I am always sad to see provably incorrect answers upvoted to a point where they cannot be deleted by the community.Ephesus
Please update to include spaces so that it answers the question.Kindergartner
L
10

I think you want preg_split:

$input = "A  B C   D";
$words = preg_split('/\s+/', $input);
var_dump($words);
Leger answered 24/11, 2009 at 21:19 Comment(0)
Y
4

instead of using explode, try preg_split: http://www.php.net/manual/en/function.preg-split.php

Yenyenisei answered 24/11, 2009 at 21:17 Comment(1)
This is not a generous post. 2009 was a sad era in terms of contribution quality for Stack Overflow.Ephesus
P
3

In order to account for full width space such as

full width

you can extend Bens answer to this:

$searchValues = preg_split("@[\s+ ]@u", $searchString);

Sources:

(I don't have enough reputation to post a comment, so I'm wrote this as an answer.)

Pitch answered 2/3, 2016 at 1:40 Comment(1)
This answer should not be used. The character class contains: 1. Whitespaces 2. Plus symbols and 3. literal spaces. There is a fundamental lack of understanding in this answer.Ephesus
E
1

Assuming $string = "\tA\t B \tC \t D "; (a mix of tabs and spaces including leading tab and trailing space)

Obviously splitting on just spaces or just tabs will not work. Don't use these:

preg_split('~ +~', $string) // one or more literal spaces, allow empty elements
preg_split('~ +~', $string, -1, PREG_SPLIT_NO_EMPTY) // one or more literal spaces, deny empty elements

preg_split('~\t+~', $string) // one or more tabs, allow empty elements
preg_split('~\t+~', $string, -1, PREG_SPLIT_NO_EMPTY) // one or more tabs, deny empty elements

Use these:

preg_split('~\s+~', $string) // one or more whitespace character, allow empty elements
preg_split('~\s+~', $string, -1, PREG_SPLIT_NO_EMPTY), // one or more whitespace character, deny empty elements

preg_split('~[\t ]+~', $string) // one or more tabs or spaces, allow empty elements
preg_split('~[\t ]+~', $string, -1, PREG_SPLIT_NO_EMPTY)  // one or more tabs or spaces, deny empty elements

preg_split('~\h+~', $string) // one or more horizontal whitespaces, allow empty elements
preg_split('~\h+~', $string, -1, PREG_SPLIT_NO_EMPTY) // one or more horizontal whitespaces, deny empty elements

A demonstration of all techniques below can be found here.

Reference Horizontal Whitespace

Ephesus answered 14/7, 2021 at 13:57 Comment(1)
This is the first answer that shows the concept of horizontal whitespaces. This is the most correct answer now, as the question asked only about tabs and spaces.Analects

© 2022 - 2025 — McMap. All rights reserved.