minify html output from php file, with a script

时间:2015-06-15 14:36:32

标签: php

I want to minify the output html from php file, I have followed this guide:

How to minify php page html output?

This is my code:

<?php

function sanitize_output($buffer) {

    $search = array(
        '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
        '/[^\S ]+\</s',  // strip whitespaces before tags, except space
        '/(\s)+/s'       // shorten multiple whitespace sequences
    );

    $replace = array(
        '>',
        '<',
        '\\1'
    );

    $buffer = preg_replace($search, $replace, $buffer);

    return $buffer;
}

ob_start("sanitize_output");

?>

The page minify, but my slider on the homepage disappears. I have nivo slider which you can see here:

https://github.com/gilbitron/nivo-slider

This is the code I have in my page for the nivo slider:

<div id="featured">
    <div id="slides">
        <div id="slider" class="nivoSlider">
            <?
            $is_first_image = TRUE;
            foreach($_info["img_slider_homepage"]["src"] as $indice=>$src)
            {
                $href       = $_info["img_slider_homepage"]["href"][$indice];
                $alt        = $_info["img_slider_homepage"]["alt"][$indice];
                $capt_id    = $_info["img_slider_homepage"]["capt_id"][$indice];
                $capt_txt   = $_info["img_slider_homepage"]["capt_txt"][$indice];
                $title      = ($capt_id
                                ? "#".$capt_id
                                : $_info["img_slider_homepage"]["title"][$indice]);

                if($href)
                {
                    ?><a href="<?=$href?>" class="<?=$is_first_image ? "" : "lazywait"?>"><img src="<?=$src?>" title="<?=$title?>" alt="<?=$alt?>" width="<?=$_info["img_slider_width"]?>" height="<?=$_info["img_slider_height"]?>"></a><?
                }
                else
                {
                    ?><img src="<?=$src?>" class="<?=$is_first_image ? "" : "lazywait"?>" title="<?=$title?>" alt="<?=$alt?>" width="<?=$_info["img_slider_width"]?>" height="<?=$_info["img_slider_height"]?>"><?
                }

                $is_first_image = FALSE;
            }
            ?>
        </div>

        <?
        foreach($_info["img_slider_homepage"]["src"] as $indice=>$src)
        {
            $capt_id    = $_info["img_slider_homepage"]["capt_id"][$indice];
            $capt_txt   = $_info["img_slider_homepage"]["capt_txt"][$indice];

            if($capt_id AND $capt_txt)
            {
                ?>
                <div id="<?=$capt_id?>" class="nivo-html-caption">
                    <?=$capt_txt?>
                </div>
                <?
            }
        }
        ?>
    </div>
</div>

this is autput html:

<body >

<div id="container"><div id="row"><div id="header_home_page"> <ul id="top-menu"><ul id="nullo" class="sf-menu"> <li><a class="tito" href="/amministra/"><img src="/inc/files/images/adminplus.png"></a><div class="ams">AMMINISTRA</div> <ul> </ul> </li></ul> <li><a href="/vacanze_weekend/">Vacanze</a></li><li><a href="/matrimoni/">Matrimoni</a></li><li><a href="/meeting/">Meeting</a></li> </li><li><span class="current-lang">ita</span> | <a href="/en/" title="English - Luxury hotels and historical boutique hotels">ENG</a></li><div id="additional-info"><div id="search-form"><form action="/chi_siamo/cerca.php" id="cse-search-box"> <input type="hidden" name="cx" value="010014129502104197540:hsefbn00beg" /> <input type="hidden" name="cof" value="FORID:11" /> <input type="hidden" name="ie" value="UTF-8" /> <input type="text" name="q" size="25" id="searchinput"/><input type="image" src="/inc/files/images/search_btn.png" id="searchsubmit" /></form></div> <!-- end #search-form --></div></ul><div class="wrap"><div id="branding"><div class="logohome">&nbsp;</div></div> </div></div><div id="additional-info"></div></div><div id="featured"><div id="slides"><div id="slider" class="nivoSlider"><img src="/inc/files/images/slider/mntr2.jpg" class="" title="" alt="" width="960" height="332"><img src="/inc/files/images/slider/cimb1.jpg" class="lazywait" title="" alt="" width="960" height="332"><img src="/inc/files/images/slider/murl2.jpg" class="lazywait" title="" alt="" width="960" height="332"><img src="/inc/files/images/slider/pvch1.jpg" class="lazywait" title="" alt="" width="960" height="332"></div></div></div><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script><script src="/inc/script/jquery.nivo.slider.pack.js" type="text/javascript"></script><script src="/inc/script/lazyload.js" type="text/javascript"></script><script type="text/javascript">$(window).load(function() { $('#slider').nivoSlider({ effect: 'random', // Specify sets like: 'fold,fade,sliceDown' animSpeed: 500, // Slide transition speed pauseTime: 3000, // How long each slide will show startSlide: 0, // Set starting Slide (0 index) directionNav: true, // Next & Prev navigation directionNavHide: true, // Only show on hover controlNav: true, // 1,2,3... navigation controlNavThumbs: true, // Use thumbnails for Control Nav controlNavThumbsFromRel: true, // Use image rel for thumbs controlNavThumbsSearch: '.jpg', // Replace this with... keyboardNav: true, // Use left & right arrows pauseOnHover: true, // Stop animation while hovering manualAdvance: false, // Force manual transitions captionOpacity: 0.8, // Universal caption opacity prevText: 'Prev', // Prev directionNav text nextText: 'Next', // Next directionNav text randomStart: false, // Start on a random slide beforeChange: function(){}, // Triggers before a slide transition afterChange: function(){}, // Triggers after a slide transition slideshowEnd: function(){}, // Triggers after all slides have been shown lastSlide: function(){}, // Triggers when last slide is shown afterLoad: function(){} // Triggers when slider has loaded });
});</script><div id="category-name" style="padding-top:38px;"> <div id="category-inner"> <h3>Siamo specializzati in Vacanze ed Eventi di Alto Livello<br>in dimore storiche di pregio con almeno 100 anni di storia</h3><p>Le nostre dimore storiche hanno in media solo 11 camere e ospitano un solo evento in esclusiva al giorno</p> </div></div> <!-- end .category-name --> ecc

1 个答案:

答案 0 :(得分:1)

滑块消失的原因是&lt;中的javascript。脚本&gt;标签包含评论。 '//'运算符会记录所有内容,直到行尾。

< script type="text/javascript">$(window).load(function() {   
$('#slider').nivoSlider({ effect: 'random', // Specify sets like: fold,fade,sliceDown' 
animSpeed: 500, // Slide transition speed pauseTime: 3000 ......

当您缩小脚本时,它基本上将所有内容插入一行。 '// Specify sets ....'之后的脚本已被移动到一行,然后被注释掉,因此忽略了之后的所有脚本。

您也可以通过删除评论来避免这种情况。可以找到javascript评论的正则表达式here