|
Home Pages Pidgin Azarennya (S|N) Mac Textanium Reference ToDo Food Local Edit Local: Hide Weather • GoWhere? • YahooMaps (mine) • GoogleMaps • Metro (map) • FairfaxConnector • GreaterGreaterWashington • DCMetrocentric • WhatsUpArlington • Restonian • BeyondDC • BaconsRebellion Language: Hide Fantasy: Hide • AnnalsOfArda • Arda • SilmarillionChronology • TolkienGateway • SciFi: Hide • CentauriDreams • ColdNavy • ConceptShips • RavenstarStudios • SkyscraperPage • StarTrek • StarTrekVsStarWars • Film: Hide IMDB • BigHollywood • DKnowsAll • Jabootu • KyleSmith Music: Hide REALbasic: Hide • Resources • Garage • University • WebRing • Forums: • REAL • ElfData • Plugins and Code: • BKeeney • DeclareSub • Einhugur • Joe • Restrepo • Tempelmann • ZAZ Coding: Hide Forums: • PowWeb • PHP • Webmaster • Coding • Walkers • Perl • Intro • Monks • PHP • JavaScript • Toolbox • UnobtrusiveJavaScript • JavaScriptCompressor • RegularExpressions (test) • JSLint • SQL • Cocoa • CocoaBuilder • CocoaDev • CocoaLab • AppleScript • BBS • Userland • Faqintosh • FileMaker • FileMakerTips • FileMakerWorld • FileMakerPlugins Science: Hide DarwinCentral • PhysOrg • PandasThumb • TalkOrigins • TalkRational • AstronomyDailyPics • Curmudgeon • SmallThings • ArchaeoBlog • AntiEvolution.org • SkepticsGuide • EvC • BadAstronomer • PhysicsForum • SlashdotScience • JunkScience • Engadget • Thunderbolts • Icecap • CentauriDreams • NewScientist • Gizmodo • CO2Science • ClimateDebate • ScienceDaily • Nrich • Math • TalkOrigins • GoodMathBadMath • Magazines • AmericanScientist • NationalGeographic • Space.com History: Hide • 1421 News/Politics: Hide WideAwakes • Anchoress • Lucianne • Strata • AceOfSpades • BigLizards • BlackAndRight • Cannonfire • DrSanity • FloppingAces • GatewayPundit • HillBuzz • HotAir • Husaria • JawaReport • JimTreacher • JsCafeNette • LittleGreenFootballs • MyVRWC • Newsbusters • Pal2Pal • PinkFlamingo • PowerLine • RachelLucas • RogerLSimon • SisterToldjah • StolenThunder • SultanKnish • TCSDaily • UppityWoman • Wizbang • NewsGroper • NewsRightNow • OriginalSignal • Blogs... Cults/Crime: Hide Miscellaneous: Hide Fun: Agony ICanHas? ObSkills Snopes Pix: Deviant Places Renderosity Blender Artists X86: OSX86 ArsTech OSNews TUAW Tools: Calculator AsciiArt XMLVal FunStuff: Pictures: Photobucket (eg Dubai) Videos: YouTube Subtitler InterestingThings: LibraryThing FlashCards GoogleDocs Wowio Bubbl.us Colemak Audible PodioBooks WonderfulInfo BooksOnline AboutUs.org |
Wiki /
ToolPHPCompressorA 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>';
?>
|