gpt4 book ai didi

php - 按空格拆分字符串

转载 作者:行者123 更新时间:2023-12-05 08:24:08 24 4
gpt4 key购买 nike

无论空格有多长,如何按空格拆分字符串?

例如,来自以下字符串:

"the    quick brown   fox        jumps   over  the lazy   dog"

我会得到一个数组

['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'];

最佳答案

您可以使用正则表达式轻松地做到这一点:

$string = 'the quick     brown fox      jumps 



over the

lazy dog';


$words = preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY);

print_r($words);

这会产生这个输出:

Array
(
[0] => the
[1] => quick
[2] => brown
[3] => fox
[4] => jumps
[5] => over
[6] => the
[7] => lazy
[8] => dog
)

关于php - 按空格拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23206953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com