The difficult part is trying to figure out what the stripwhitespace() function does. stripbuffer() is fairly straightforward, but I've been staring at this little piece of code for a while now trying to decipher it, but to no avail. The cryptic variable names and lack of comments don't help much, either. I also had to remove some hyperlinks from the credits because of the spam prevention on this site
<?php
/* ---------------------------------
26 January, 2008 - 2:55pm:
The example below is adapted from a post by londrum 8:29 pm on June 7, 2007:
"crunch up your HTML into a single line
a handy little script..."
This PHP code goes at the very TOP of the PHP-enabled HTML webpage
above EVERYTHING else. Recommendation: use a PHP include file for this
to have only one file to maintain.
--------------------------------- */
function stripwhitespace($bff){
$pzcr=0;
$pzed=strlen($bff)-1;
$rst="";
while($pzcr<$pzed){
$t_poz_start=stripos($bff,"<textarea",$pzcr);
if($t_poz_start===false){
$bffstp=substr($bff,$pzcr);
$temp=stripBuffer($bffstp);
$rst.=$temp;
$pzcr=$pzed;
}
else{
$bffstp=substr($bff,$pzcr,$t_poz_start-$pzcr);
$temp=stripBuffer($bffstp);
$rst.=$temp;
$t_poz_end=stripos($bff,"</textarea>",$t_poz_start);
$temp=substr($bff,$t_poz_start,$t_poz_end-$t_poz_start);
$rst.=$temp;
$pzcr=$t_poz_end;
}
}
return $rst;
}
function stripBuffer($bff){
/* carriage returns, new lines */
$bff=str_replace(array("\r\r\r","\r\r","\r\n","\n\r","\n\n\n","\n\n"),"\n",$bff);
/* tabs */
$bff=str_replace(array("\t\t\t","\t\t","\t\n","\n\t"),"\t",$bff);
/* opening HTML tags */
$bff=str_replace(array(">\r<a",">\r <a",">\r\r <a","> \r<a",">\n<a","> \n<a","> \n<a",">\n\n <a"),"><a",$bff);
$bff=str_replace(array(">\r<b",">\n<b"),"><b",$bff);
$bff=str_replace(array(">\r<d",">\n<d","> \n<d",">\n <d",">\r <d",">\n\n<d"),"><d",$bff);
$bff=str_replace(array(">\r<f",">\n<f",">\n <f"),"><f",$bff);
$bff=str_replace(array(">\r<h",">\n<h",">\t<h","> \n\n<h"),"><h",$bff);
$bff=str_replace(array(">\r<i",">\n<i",">\n <i"),"><i",$bff);
$bff=str_replace(array(">\r<i",">\n<i"),"><i",$bff);
$bff=str_replace(array(">\r<l","> \r<l",">\n<l","> \n<l","> \n<l","/>\n<l","/>\r<l"),"><l",$bff);
$bff=str_replace(array(">\t<l",">\t\t<l"),"><l",$bff);
$bff=str_replace(array(">\r<m",">\n<m"),"><m",$bff);
$bff=str_replace(array(">\r<n",">\n<n"),"><n",$bff);
$bff=str_replace(array(">\r<p",">\n<p",">\n\n<p","> \n<p","> \n <p"),"><p",$bff);
$bff=str_replace(array(">\r<s",">\n<s"),"><s",$bff);
$bff=str_replace(array(">\r<t",">\n<t"),"><t",$bff);
/* closing HTML tags */
$bff=str_replace(array(">\r</a",">\n</a"),"></a",$bff);
$bff=str_replace(array(">\r</b",">\n</b"),"></b",$bff);
$bff=str_replace(array(">\r</u",">\n</u"),"></u",$bff);
$bff=str_replace(array(">\r</d",">\n</d",">\n </d"),"></d",$bff);
$bff=str_replace(array(">\r</f",">\n</f"),"></f",$bff);
$bff=str_replace(array(">\r</l",">\n</l"),"></l",$bff);
$bff=str_replace(array(">\r</n",">\n</n"),"></n",$bff);
$bff=str_replace(array(">\r</p",">\n</p"),"></p",$bff);
$bff=str_replace(array(">\r</s",">\n</s"),"></s",$bff);
/* other */
$bff=str_replace(array(">\r<!",">\n<!"),"><!",$bff);
$bff=str_replace(array("\n<div")," <div",$bff);
$bff=str_replace(array(">\r\r \r<"),"><",$bff);
$bff=str_replace(array("> \n \n <"),"><",$bff);
$bff=str_replace(array(">\r</h",">\n</h"),"></h",$bff);
$bff=str_replace(array("\r<u","\n<u"),"<u",$bff);
$bff=str_replace(array("/>\r","/>\n","/>\t"),"/>",$bff);
$bff=ereg_replace(" {2,}",' ',$bff);
$bff=ereg_replace(" {3,}",' ',$bff);
$bff=str_replace("> <","><",$bff);
$bff=str_replace(" <","<",$bff);
/* non-breaking spaces */
$bff=str_replace(" "," ",$bff);
$bff=str_replace(" "," ",$bff);
/* Example of EXCEPTIONS where I want the space to remain
between two form buttons at */
/* <!-- http://websitetips.com/articles/copy/loremgenerator/ --> */
/* name="select" /> <input */
$bff=str_replace(array("name=\"select\" /><input"),"name=\"select\" /> <inp开发者_开发技巧ut",$bff);
return $bff;
}
ob_start("stripwhitespace");
?>
It looks to me as if it crunches everything before the textarea and after the textarea but it leaves the contents of a textarea alone.
While this code may be somewhat interesting, PHP is notoriously bad at fast string manipulation and all those str_replace calls are a bad, bad idea.
I predict you'd get better performance by using gzip/deflate on the web server to compress the script output before sending.
It's definitely a mess, but it seems as if it strips unnecessary white-space from a string, except from within textareas.
It's obvious what stripBuffer
does: it tries to strip all whitespace from its input.
stripwhitespace
works as follows:
function stripwhitespace($input){
$currentPosition=0; // start from the first char
$endPosition=strlen($input)-1; // where to stop
$returnValue="";
// while there is more input to process
while($currentPosition<$endPosition){
// find start of next <textarea> tag
$startOfNextTextarea=stripos($input,"<textarea",$currentPosition);
if($startOfNextTextarea===false){
// no textarea tag remaining:
// strip ws from remaining input, append to $returnValue and done!
$bufferToStrip=substr($input,$currentPosition);
$temp=stripBuffer($bufferToStrip);
$returnValue.=$temp;
$currentPosition=$endPosition; // to cause the function to return
}
else{
// <textarea> found
// strip ws from input in the range [current_position, start_of_textarea)
$bufferToStrip=substr($input,$currentPosition,$startOfNextTextarea-$currentPosition);
// append to return value
$temp=stripBuffer($bufferToStrip);
$returnValue.=$temp;
$endOfNextTextarea=stripos($input,"</textarea>",$startOfNextTextarea);
// get contents of <textarea>, append to return value without stripping ws
$temp=substr($input,$startOfNextTextarea,$endOfNextTextarea-$startOfNextTextarea);
$returnValue.=$temp;
// continue looking for textareas after the end of this one
$currentPosition=$endOfNextTextarea;
}
}
return $returnValue;
}
I admit this would be quite harder if you can't "intuitively" tell what it's trying to do, given the special treatment the content of <textarea>
tags gets in HTML.
In pseudo code (ish)
bff is the initial buffer
pzcr is the current start
pzed is the current end
rst will have the filtered text appended to it.
while the current start is before the end
t_pos_start is first position of the textarea (after current start)
if there is no text area found
bffstp becomes the substring of the buffer starting at pzcr
temp is buffer stripped.
append temp to rst
set the current start to the current end.
else
set bffstp to the substr between the start and the start of the textarea tag
temp is buffer stripped.
append temp to rst
skip the textarea
temp will be the substr from the start of the text area to the closing text area tag.
append temp (unfiltered) to rst.
set the next start to the end of the textarea (at the start of its closing tag).
end the if
end the while
return the appended buffer (rst)
Hmm - As an html compressor, this code itself is actually bloated as well as hard to read. Regular expressions, used well, should be able to make a much better job of this.
精彩评论