note 40349 added to ref.strings
| From: | warhog at warhog dot net | Date: | Sun, 29 Feb 2004 23:36:22 +0000 |
| Subject: | note 40349 added to ref.strings | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-66011@lists.php.net to get a copy of this message | ||
this script will strip all comments, line breaks and double spaces which are not in a string
quotation with double-quots and create a new file from this.
set the QUERY_STRING to the file which you want to be converted, it will rebuild the file in
whg.filename and tell you about the new size and the ratio.
worked with a lot of scripts and made them littler and more efficent for me, maybe it's just a
little fun and nervy for users who want to read ur filez :D
here the code (of course, without linebreaks and so one, works great ;) ):
<?php $f = @fopen($QUERY_STRING, "r") or die("couldn't retrieve file.");
$l = $o = ""; $i = -1; $is = array("php" => FALSE, "string" =>
FALSE, "comment" => FALSE, "olc" => FALSE); while(!feof($f)) { $i++; $c =
$oc = fread($f, 1); if ($l == "<" && $c == "?" &&
!$is['php']) { $is['php'] = TRUE; } elseif ($l == "?" && $c ==
">" && $is['php'] && !$is['string']) {
$is['php'] = FALSE; } if ($c == "\"" && !$is['string']
&& $is['php'] && !$is['olc']) { $is['string'] = TRUE;
} elseif ($l != "\\" && $c == "\"" &&
$is['string'] && !$is['olc'] && $is['php']) {
$is['string'] = FALSE; } if ($is['php'] && !$is['string']
&& !$is['comment'] && !$is['olc'] && $c == "*"
&& $l == "/") { $is['comment'] = TRUE; } elseif
($is['comment'] && $c == "/" && $l == "*" &&
!$is['olc']) { $is['comment'] = FALSE; $o{strlen($o)-1} = " "; $c =
""; } if ($is['php'] && !$is['string'] &&
!$is['comment'] && $c == "/" && $l == "/" &&
!$is['olc']) { $is['olc'] = TRUE; $o{strlen($o)-1} = " "; $c =
""; } if ($is['php'] && !$is['string'] &&
!$is['comment'] && $c == "#" && !$is['olc']) {
$is['olc'] = TRUE; $c = ""; } if ($is['olc'] && $c ==
"\n") { $is['olc'] = FALSE; } if (!$is['string'] &&
!$is['comment']) { if ($c == "\n" || $c == "\r") { $c = $oc = "
"; } if ($c == " " && $o{strlen($o)-1} == " ") { $c = "";
} } if ($is['comment'] || $is['olc']) { $c = ""; } if ($c !=
"") { $o .= $c; } $l = $oc; } $ni = strlen($o); fclose($f); $f =
fopen("whg.$QUERY_STRING", "w"); fwrite($f, $o, strlen($o)); fclose($f); echo
"orig: ".($i/1024)."k; now: ".($ni/1024)."k; ratio: ".($i-$ni).";
stored to \"whg.$QUERY_STRING\""; ?>
?>
----
Manual Page -- http://www.php.net/manual/en/ref.strings.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40349
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40349&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40349&report=yes
Search -- http://master.php.net/manage/user-notes.php