首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这个HTML优化代码的开头是做什么的?

这个HTML优化代码的开头是做什么的?
EN

Stack Overflow用户
提问于 2010-12-17 19:59:57
回答 4查看 237关注 0票数 0

困难的部分是试图找出strip空格()函数的作用。stripbuffer()非常简单,但我已经盯着这段代码看了一段时间了,试图对其进行解密,但没有结果。神秘的变量名和缺少注释也没有多大帮助。我还不得不从学分中删除一些超链接,因为在这个网站上防止了垃圾邮件。

代码语言:javascript
复制
<?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(" &nbsp;","&nbsp;",$bff);
    $bff=str_replace("&nbsp; ","&nbsp;",$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\" /> <input",$bff);

    return $bff;
}
ob_start("stripwhitespace");
?>
EN

回答 4

Stack Overflow用户

发布于 2010-12-17 20:10:20

在我看来,它似乎压缩了文本区域之前和文本区域之后的所有内容,但它只保留文本区域的内容。

虽然这段代码可能有点有趣,但PHP在快速字符串操作方面却是出了名的糟糕,所有这些str_replace调用都是一个糟糕的主意。

我预测通过在web服务器上使用gzip/deflate在发送之前压缩脚本输出,您将获得更好的性能。

票数 2
EN

Stack Overflow用户

发布于 2010-12-17 20:07:44

这绝对是一团糟,但似乎它从字符串中删除了不必要的空白,除了文本区域。

票数 1
EN

Stack Overflow用户

发布于 2010-12-17 20:22:17

很明显,stripBuffer做了什么:它试图从它的输入中删除所有空白。

stripwhitespace的工作如下:

代码语言:javascript
复制
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;
}

我承认,如果您不能“直观地”告诉它要做什么,这将非常困难,因为<textarea>标记的内容在HTML中得到了特殊处理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4474322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档