コード例 #1
0
/*  Example: */
$parse_this = "This text has some \"DOBLE QUOTED PARTS WITH \$vars AND \\e\\s\\capes ON IT\", along with some 'single quoted text too with futile e\\s\\c\\a\\p\\e\\s'\"(because they don't work on single quotes)\" and some \$variable_names\n";
echo $parse_this;
$triggers = array("\\" => "escape", '"' => "doublequote", "'" => "singlequote", '$' => "dollarsign");
// first character intentionally left blank to add 1 to position count of the
// rest
$alpha = " abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";
$stored = "";
for ($i = 0; $i < strlen($parse_this); $i++) {
    $char = $parse_this[$i];
    if (isset($triggers[$char])) {
        $state->trigger($triggers[$char]);
    } else {
        $state->trigger(strpos($alpha, $char) ? "alpha" : "nonalpha");
    }
    if (!$state->changed() || $state->previous() == "escaped") {
        $stored .= $char;
    } elseif ($state->state() == "escaped") {
    } else {
        switch ($state->previous()) {
            case "idle":
                $stored = "";
                break;
            case "symbol":
                echo "--> symbol: " . $stored . "\n";
                $stored = "";
                break;
            case "variable":
                echo "--> variable: " . $stored . "\n";
                $stored = "";
                break;