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.
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.
No need for the relatively slow regex functionality.