Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explode string by one or more spaces or tabs

Tags:

regex

php

explode

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

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


This works:

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

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

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

Note: you must used double quote, not single.


I think you want preg_split:

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