0

我的 AJAX 聊天处理以下命令:

/禁止名称

/锁

和更多。

如果我使用多个单词,像这样:

/广播大家好!

输出将是:

你好

我的问题是:如何使它在命令之后不换行(剪切)文本?

所以当我使用这个命令时:/broadcast 大家好,哈哈哈!

输出将是:大家好,哈哈哈!

代替:你好

这是命令的方法:

    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;
4

2 回答 2

0

更改这行代码

    if (isset($str[1]))
    {
        $name = $str[1];
    }

   if (is_array($str)) {
      $name = array_shift($str);
      $name = implode(" ", $name);
   }
于 2013-06-22T12:43:11.797 回答
0

发现了问题。需要设置一个限制爆炸功能:

$str = explode(' ', $message, 2);
于 2013-06-22T19:47:50.490 回答