Explode string by one or more spaces or tabs

PhpRegexExplode

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

$parts = preg_split('/\s+/', $str);

Solution 2 - Php

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);

Solution 3 - Php

This works:

$string = 'A   B C          D';
$arr = preg_split('/\s+/', $string);

Solution 4 - Php

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

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

Note: you must used double quote, not single.

Solution 5 - Php

I think you want preg_split:

$input = "A  B C   D";
$words = preg_split('/\s+/', $input);
var_dump($words);

Solution 6 - Php

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

Solution 7 - Php

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

Solution 8 - Php

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, allow 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarthVaderView Question on Stackoverflow
Solution 1 - PhpBen JamesView Answer on Stackoverflow
Solution 2 - PhpAliAvciView Answer on Stackoverflow
Solution 3 - PhpschneckView Answer on Stackoverflow
Solution 4 - PhplucsanView Answer on Stackoverflow
Solution 5 - PhpjheddingsView Answer on Stackoverflow
Solution 6 - PhpBrian SchrothView Answer on Stackoverflow
Solution 7 - PhpMPSView Answer on Stackoverflow
Solution 8 - PhpmickmackusaView Answer on Stackoverflow