All pastes #253703 Raw Edit

Miscellany

public text v1 · immutable
#253703 ·published 2006-11-21 00:29 UTC
rendered paste body
	function parse_opts ($string) {
		$chars = array();
		$breaks = array(" ","\n","\r","\t");
		$quotes = array("\"","'");

		$currtoken = "";

		$tokens = array();

		$InQuote = FALSE;
		$Escaped = FALSE;


		$chars = strsplit($string);
		foreach ($chars as $thischar) {
			if ($InQuote) {
				if ((!$Escaped) && ($thischar == $InQuote)) {
					$InQuote = FALSE;
				} else if ($thischar == "\\") {
					if ($Escaped) {
						$currtoken .= $thischar;
						$Escaped = FALSE;
					} else {
						$Escaped = TRUE;
					}
				} else {
					$currtoken .= $thischar;
					$Escaped = FALSE;
				}
			} else if (in_array($thischar,$quotes)) {
				$InQuote = $thischar;
			} else if (in_array($thischar,$breaks)) {
				if (!empty($currtoken)) {
					$tokens[] = $currtoken;
					$currtoken = "";
				}
			} else {
				$currtoken .= $thischar;
			}


		}
		if (!empty($currtoken)) {
			$tokens[] = $currtoken;
		}

		return $tokens;
	}