Skip to content

Instantly share code, notes, and snippets.

@Zegnat
Created March 6, 2013 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zegnat/5102552 to your computer and use it in GitHub Desktop.
Save Zegnat/5102552 to your computer and use it in GitHub Desktop.
<?php
// This assumes $text is the input we are parsing.
// I am using normal `echo`s here to output text, you will use whatever function you need.
if (preg_match('/^\/ba?ck(?:\s+(.*?))?\s*$/', $text, $result)) {
// $text matched!
if (isset($result[1]) && strlen($result[1]) > 0) {
// $text included an optional back message to display.
echo '<p><strong>Back:</strong> ' . $result[1] .'</p>';
} else {
// $text did not include any message, just show the default.
echo '<p><strong>Back.</strong></p>';
}
}
?>

What we look for:

/bck
/back
/bck Got my coffee.
/back Done in the bathroom.

Anti-whitespace:

The command (/bck or /back) can be followed by any amount of whitespace, this will happily be ignored. Even when a message is supplied all whitespace between the command and the message is stripped. This means all of the following commands are the same:

/bck     Baby stopped crying.
/back               Baby stopped crying.                      

Whitespace before the actual command is not allowed. This will not parse:

     /bck
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment