/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /** * Parses a String of Tags * * Tags are space delimited. Either single or double quotes mark a phrase. * Odd quotes will cause everything on their right to reflect as one single * tag or phrase. All white-space within a phrase is converted to single * space characters. Quotes burried within tags are ignored! Duplicate tags * are ignored, even duplicate phrases that are equivalent. * * Returns an array of tags. */ function ParseTagString($sTagString) { $cPhraseQuote = null; // Record of the quote that opened the current phrase $sPhrase = null; // Temp storage for the current phrase we are building // Define some constants static $sTokens = " \t"; // Space, Return, Newline, Tab static $sQuotes = "'\""; // Single and Double Quotes // Start the State Machine do { // Get the next token, which may be the first // Are there more tokens? if ($sToken === false) { // Ensure that the last phrase is marked as ended $cPhraseQuote = null; } else { // Are we within a phrase or not? if ($cPhraseQuote !== null) { // Will the current token end the phrase? { // Trim the last character and add to the current phrase, with a single leading space if necessary $cPhraseQuote = null; } else { // If not, add the token to the phrase, with a single leading space if necessary } } else { // Will the current token start a phrase? { // Will the current token end the phrase? { // The current token begins AND ends the phrase, trim the quotes } else { // Remove the leading quote $cPhraseQuote = $sToken[0]; } } else $sPhrase = $sToken; } } // If, at this point, we are not within a phrase, the prepared phrase is complete and can be added to the array if (($cPhraseQuote === null) && ($sPhrase != null)) { $sPhrase = null; } } while ($sToken !== false); // Stop when we receive FALSE from strtok() return $arTags; }