PHP script to read in user action requests and parse them to their components. Example, user types in SET Colour = Blue or describe Chocolate Cake = The best cake ever! I’m using like this:
$actionKeyword = strtok( $actionRequest, " " ); // keyword followed by space
$name = strtok( "=" ); // Then name followed by equals
$description = strtok(null); // get the rest of the string
I could not find anything on getting the rest of the string. PHP.net’s example was using spaces to tokenize each word but there was no character I could think of that might not be part of the description. This solution works in my tests.
Is there a side effect or special case this would fail on? Or is this a perfectly safe and acceptable way of getting the rest of the line?
It shouldn’t cause any bugs now, as
nullgets converted to an empty string whenstrtokparses its parameters.But you probably will be a bit safer if you use
strtok('')form explicitly. The reason why it works is thatstrtokexpects a stringified list of delimiter characters as a token param. So an empty string here is basically an empty list of delimiters. And no delimiters to look for means the whole remaining string to return. )By the way, this advice is given in comments at the manual page. )