在每个空格上拆分一个字符串,直到找到第二个冒号


Split a string on every space untill the second colon is found

我一直在寻找一种在每个空格上拆分字符串的方法,直到找到第二个冒号。如果它对答案有帮助,我这样做的语言是PHP。基本上我想要的是以下字符串:

:foo!bar@cookie.net strawberry cheesecake :hello world: this is a message!

拆分为以下数组:

array(
    [0] => ":foo!bar@cookie.net",
    [1] => "strawberry",
    [2] => "cheesecake",
    [3] => ":hello world: this is a message!"
);

我将如何实现这一目标?请注意,不能保证冒号在那里。输入字符串可能不同,例如 Hello World .

你首先会在冒号上分裂。然后在空间上拆分第一个

$colon = explode(",",$str);
$space = explode(" ",$colon[1]);

然后冒号[2]将有字符串的其余部分,空间将有空间分隔的部分