我的AJAX聊天处理如下命令:
/禁止名称
/锁定
等等。
如果我使用多个单词,就像这样:
/广播大家好!
输出将是:
helloe
我的问题是: 在命令之后,如何使它不包裹(剪切)文本?
所以当我使用这个命令:/ broadcast hello everyone,哈哈哈!
输出将是:大家好,哈哈哈!
而不是: helloe
这是命令的方法:
public function handleCommands($message, $username)
{
// Splits the message.
$str = explode(' ', $message);
// Gets every space of the message, basically this is the command that comes after the slash
$command = substr(strrchr($str[0], '/'), 1);
/**
* If we have a value after the command:
**/
if (isset($str[1]))
{
$name = $str[1];
}
switch ($command)
{
case 'ban':
if(!empty($name))
{
if (ctype_alpha($name))
{
$this->ban($name, $username);
}
else
{
echo "Syntax Error. Do not use numbers or special characters.";
break;
}
}
else
{
echo "Syntax Error. usage: /ban (User name)";
break;
}
break;
case 'prune':
$this->prune($username);
break;
case '':
echo 'Available commands: /ban, /prune';
break;
case 'lock':
try
{
$this->lockChat($username);
}
catch (exception $r)
{
echo $r->getMessage();
}
break;
case 'broadcast':
echo $name;
break;
case 'unlock':
try
{
$this->unLockChat($username);
}
catch (exception $r)
{
echo $r->getMessage();
}
break;
default:
echo 'That command does not exist!';
break;
}
}
有什么想法吗?
问题出在这种情况下:
case 'broadcast':
echo $name;
break;
答案 0 :(得分:0)
更改此行代码
if (isset($str[1]))
{
$name = $str[1];
}
到
if (is_array($str)) {
$name = array_shift($str);
$name = implode(" ", $name);
}
答案 1 :(得分:0)
发现问题。 需要设置爆炸功能的限制:
$str = explode(' ', $message, 2);