如何通过白色空间分割字符串无论白色空间有多长?
例如,从以下字符串:
"the quick brown fox jumps over the lazy dog"
Run Code Online (Sandbox Code Playgroud)
我会得到一个数组
['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'];
Run Code Online (Sandbox Code Playgroud)
您可以使用正则表达式轻松完成此操作:
$string = 'the quick brown fox jumps
over the
lazy dog';
$words = preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY);
print_r($words);
Run Code Online (Sandbox Code Playgroud)
这会产生以下输出:
Array
(
[0] => the
[1] => quick
[2] => brown
[3] => fox
[4] => jumps
[5] => over
[6] => the
[7] => lazy
[8] => dog
)
Run Code Online (Sandbox Code Playgroud)