【问题标题】:AJAX Chat commands /broadcast <text> command?AJAX 聊天命令 /broadcast <text> 命令?
【发布时间】:2013-06-22 12:37:28
【问题描述】:

我的 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;

【问题讨论】:

    标签: php oop


    【解决方案1】:

    修改这行代码

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

       if (is_array($str)) {
          $name = array_shift($str);
          $name = implode(" ", $name);
       }
    

    【讨论】:

    • 警告:implode() [function.implode]:第 56 行 C:\xampp\htdocs\chat\includes\class\Commands.class.php 中传递的参数无效
    【解决方案2】:

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

    $str = explode(' ', $message, 2);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 2016-08-25
      • 2021-11-18
      • 2014-02-05
      相关资源
      最近更新 更多