Get a specific value from a string typed by the user

I basically want to create a ban command that will work when the user types this command on the input:

/ban [nick]

And I want to get the [nick] as a variable. How do I do that?

Here is my code

$message = mysqli_real_escape_string($conection, $POST['message']);
if($message == "/ban $nick") {
echo ("The user that you want to ban is:".$nick);
}

The $nick is not defined cause it’s exactly the variable that I want to get from the command.

Answer

You could use Regex patterns here but I’ll take an approach of removing just the /ban if it’s found in the message string.

$message = "/ban hppycoder";
if (stristr($message, "/ban ") !== false) {
    $nick = str_replace("/ban ", "", $message);
    echo sprintf("The user you want to ban is: %s", $nick);
}

Output:

The user you want to ban is: hppycoder

$message = "/say hello to hppycoder"; produces no output because it does not contain /ban