我正在寻找一种将下面的字符串转换为数组的方法

我试过这样的方法:
$myArray = [];
$lines = explode(PHP_EOL, $text);
$l = 0;
foreach($lines as $line) {
//$myArray[$l] = array_map('trim', explode(" ", $line));
//$myArray[$l] = preg_split("/\s*[" . preg_quote(implode("", array(' ')), "/") . "]+\s*/", $line);
$myArray[$l] = preg_split('/[ ,]/', $line, null, PREG_SPLIT_NO_EMPTY); // or '/ |,/'
$l++;
}
dd($myArray);但是,所有这些都只是在数组之间去掉有空格的单词。我希望系统重新划分超过2个空间,并使用它作为一个分割和修剪其余的空间。
我在堆栈溢出网站上找不到类似的问题。
发布于 2019-07-25 11:59:39
使用"/\s{2,}/"至少使用两个空格拆分:
$str = "hello world so much space around me, and even tabs a b c final part";
var_dump(preg_split("/\s{2,}/", $str));产出:
array(4) {
[0]=>
string(11) "hello world"
[1]=>
string(38) "so much space around me, and even tabs"
[2]=>
string(5) "a b c"
[3]=>
string(10) "final part"
}https://stackoverflow.com/questions/57201368
复制相似问题