Skip to content

Instantly share code, notes, and snippets.

@enigmaticape
Created December 2, 2012 23:11
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 enigmaticape/4191521 to your computer and use it in GitHub Desktop.
Save enigmaticape/4191521 to your computer and use it in GitHub Desktop.
The little tokeniser that could
<?php
function tokeniseString( $string ) {
$regex = "/([[:space:]]+)|([[:punct:]])/";
$opts = PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY;
$toks = preg_split( $regex, $string, -1, $opts );
return $toks;
}
$string = "[various words!] 2 + (thirty thirsty_badgers), '9^7' | 6 >=2 ";
var_dump( tokeniseString( $string ) );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment