如何在 php 中查找字符串的第二个空格


How to find the second whitespace of a string in php?

$string = "/kick <user> (time)"; //time is optional, user is required
if (!strpos($string, ' ')) {
    $firstSpace = strlen($string);
} else {
    $firstSpace = strpos($string, ' ');
}

我正在设计一个AJAX实时支持聊天室。使用/command我可以做诸如禁止用户,显示帮助等操作,但是由于问题,我无法踢用户。

为了获得<user>,我使用以下公式来查找第一个空格。
如何使用 php 才能<time>使用 strpos?

您可以使用

explode()函数:

$string = "/kick <user> (time)"; //because <user> is mandatory whereas (time) is optional
$pieces = explode(" ", $string);
echo $pieces[0]; // kick
echo $pieces[1]; // <user>
echo $pieces[2]; // (time)

使用偏移量参数:

strpos($string, ' ', strpos($string, ' '));

官方文档