Recent Changes - Search:

Home Pages Pidgin   Azarennya (S|N) Mac Textanium Reference ToDo Food Local Edit

Local: Hide

Language: Hide

Fantasy: Hide

SciFi: Hide

Film: Hide

Music: Hide

REALbasic: Hide

ResourcesGarageUniversityWebRingForums:REALElfDataPlugins and Code:BKeeneyDeclareSubEinhugurJoeRestrepoTempelmannZAZ

Coding: Hide

Forums:PowWebPHPWebmasterCodingWalkersPerlIntroMonksPHPJavaScriptToolboxUnobtrusiveJavaScriptJavaScriptCompressorRegularExpressions (test)JSLintSQLCocoaCocoaBuilderCocoaDevCocoaLabAppleScriptBBSUserlandFaqintoshFileMakerFileMakerTipsFileMakerWorldFileMakerPlugins

Science: Hide

History: Hide

1421

News/Politics: Hide

Cults/Crime: Hide

ClambakeInfidels

Miscellaneous: Hide

ToolPHPCompressor

A quick and dirty script to remove comments: Go through the code character by character, checking for a quote mark. Process all text up to that first quote mark; do nothing with text immediately after the quote mark, but instead search for the next quote mark of the same kind (not preceded by a backslash). Once you find the closing quote, process the text that follows. Do this until you hit the end of the text.

<?php
// QUICK AND DIRTY PHP CODE COMPRESSOR

// WARNING: Won't work as written with "heredoc" strings!
// Always TEST code compressed with this script before relying on it.

$text = $_POST['text'];
$comment = '';
$count = strlen($text);
$quote = '';
$prev = '';

for ($i = 0; $i < $count; ++$i) {
	$c = $text{$i};

	// If the comment flag is set, don't do anything until the
	// end-of-comment mark is found.

	if ($comment == "//") {
		if ($c == "\n") {
			$comment = '';
			//$compressed .= ' ';
		}
	} elseif ($comment == "/*") {
		if ($prev == '*' and $c == '/') {
			$comment = '';
			//$compressed .= ' ';
		}
	} else {

		// If we are outside quotes, then we can check for a comment mark
		// and add characters to the code string to be processed.

		if ($quote == '') {

			// If we found the marker for the start of a comment,
			// set the comment flag.

			if ($prev == '/') {
				if ($c == '/') {
					$comment = "//";
				} elseif ($c == '*') {
					$comment = "/*";
				}

				// If the slash we held back was NOT the start of a
				// comment, go ahead and add it to the code string.

				else {
					$code .= $prev;
				}
				$prev = '';
			}

			if ($comment == '') { // If the comment flag was NOT just set above...

				// If a single or double quote mark has been found,
				// set the quote flag, process the code accumulated so far,
				// and append it and the quote mark to the output.

				if ($c == "'" or $c == '"' or $i == $count - 1) {
					$quote = $c;

					while (strpos($code, ". ") !== FALSE)
						$code = str_replace(". ", ".", $code);
					while (strpos($code, " .") !== FALSE)
						$code = str_replace(" .", ".", $code);
					while (strpos($code, ", ") !== FALSE)
						$code = str_replace(", ", ",", $code);
					while (strpos($code, " ,") !== FALSE)
						$code = str_replace(" ,", ",", $code);
					while (strpos($code, "; ") !== FALSE)
						$code = str_replace("; ", ";", $code);
					while (strpos($code, " ;") !== FALSE)
						$code = str_replace(" ;", ";", $code);
					while (strpos($code, "+ ") !== FALSE)
						$code = str_replace("+ ", "+", $code);
					while (strpos($code, " +") !== FALSE)
						$code = str_replace(" +", "+", $code);
					while (strpos($code, "/ ") !== FALSE)
						$code = str_replace("/ ", "/", $code);
					while (strpos($code, " /") !== FALSE)
						$code = str_replace(" /", "/", $code);
					while (strpos($code, "= ") !== FALSE)
						$code = str_replace("= ", "=", $code);
					while (strpos($code, " =") !== FALSE)
						$code = str_replace(" =", "=", $code);
					while (strpos($code, "( ") !== FALSE)
						$code = str_replace("( ", "(", $code);
					while (strpos($code, " (") !== FALSE)
						$code = str_replace(" (", "(", $code);
					while (strpos($code, ") ") !== FALSE)
						$code = str_replace(") ", ")", $code);
					while (strpos($code, " )") !== FALSE)
						$code = str_replace(" )", ")", $code);
					while (strpos($code, "{ ") !== FALSE)
						$code = str_replace("{ ", "{", $code);
					while (strpos($code, " {") !== FALSE)
						$code = str_replace(" {", "{", $code);
					while (strpos($code, "} ") !== FALSE)
						$code = str_replace("} ", "}", $code);
					while (strpos($code, " }") !== FALSE)
						$code = str_replace(" }", "}", $code);
					while (strpos($code, "  ") !== FALSE)
						$code = str_replace("  ", " ", $code);

					$code = trim($code);

					$compressed .= $code;
					if ($prev == '/') $compressed .= $prev;
					$compressed .= $c;
					$code = '';
				}

				// If the character is whitespace, make sure it is a literal
				// space. Append the space to the accumulated code only if
				// the previous character was not also whitespace.

				elseif ($c == "\n" or $c == "\r" or $c == "\t" or $c == ' ') {
					$c = ' ';
					if ($prev != ' ') $code .= $c;
				}

				// If the character is a slash, DON'T add it yet, as it might be
				// the start of a comment mark.

				elseif ($c == '/') {
				}

				// If the character is anything else, just append it to the code
				// accumulated so far.

				else {
					$code .= $c;
				}
			}
		}

		// If we are inside quotes, then we can't process characters;
		// we can only add them directly to output.

		else { // we're within quotes
			if ($c == $quote) {
				$quote = '';
			}
			$compressed .= $c;
		}
	}
	$prev = $c;
}
echo '<html><head><title>PHP Code Compressor Results</title></head><body>';
echo '<h1>PHP Code Compressor: Results</h1>';
echo '<form action="' . basename($_SERVER['PHP_SELF']) . '" method="post" target="_self">';
echo '<textarea name="text" rows="30" cols="80">';
echo htmlspecialchars(trim($compressed));
//echo htmlspecialchars($xx);
echo '</textarea><br /><input type="submit" name="submit" value="Compress">';
echo '</form></body></html>';
?>
Edit - History - Print - Recent Changes - Search
Page last modified on February 04, 2008, at 02:14 PM