PHP Regex get command from a chat

303 views Asked by At

Im currently developing a AJAX based chat, and i'm trying to get to the part where i can input commands in the chat as in this example:

"/roll 20" will give a random number between 1 and 20.

And i thought it would be best to fetch commands with regex, but i cant get the values from the string and this is what i've tried.

$message = "/roll 20";
preg_match("/roll \d+", $message, $matches);
print_r($matches);

I would like to get "/roll 20" from the message but it wont print any matches. So is it that my regex is wrong or am i handling the preg_match wrong?

ps. If you have another PHP based style to fetch commands with, i'l gladly take a look.

4

There are 4 answers

1
Mike Brant On BEST ANSWER

I honestly don't see why you would use regex for this. I assume you only have a fixed set of commands against which you might be matching, and that you would only issue such commands when the input starts with /. And that the command would need to come as first item in line and have parameters to the command separated by spaces.

So, using preg_match() you would need to have a series of patterns that you loop through to compare the line. This could become bulky to maintain.

I would propose a different parser-like approach, using explode to break apart the input line, inspecting the first element to see if it is a command. If so, you collect the parameters and call a function to do what you want to do.

$input = '/roll 20';
$commands = array(
    'roll' => array('range'), // single parameter
    'foo' => array('param1', 'param2'), // multiple parameters
    'bar' => array() // no parameters
);  

$input_array = explode(' ', $input);
$command = ltrim($input_array[0], '/');
if (strpos($input_array[0], '/') === 0 && array_key_exists($command, $commands)) {
    $parameters = array();
    $i = 1;
    foreach($commands[$command] as $param_key) {
        if(empty($input_array[$i])) {
            // error we did not get expected number of parameters
            // handle this error somehow
            die('Not enough parameters passed for command');
        } else {
            $parameters[$param_key] = $input_array[$i];
            $i++;
        }
    }
    // perhaps have a function defined for each command that you can call
    // while passing in the parameter array
    call_user_func($command, $parameters);
} else {
    // this is just text content
    // do whatever
}

No need for the relatively slow regex functionality.

0
Sabuj Hassan On

Use delimiter for your regex:

preg_match("~/roll \d+~", $message, $matches);
            ^         ^
1
Rakesh KR On

Use Delimiter

preg_match("#/roll \d+#", $message, $matches);
0
Curtis W On

You'd need to use delimiters around the regex and also add regex to catch the beginning and end of a string. I've also wrapped the roll amount in parameters so you get a variable for the roll amount. So a phrase with the /roll command in the middle of the input wouldn't be caught.

$message = "/roll 20";
preg_match("#^/roll (\d+)$#", $message, $matches);
print_r($matches);