rusly
rusly

Reputation: 1522

Remove line break but keep javascript format

Below is code how i remove multiple spaces from the buffer :

function removeWhitespace($buffer)
{
   return preg_replace('/\s+/', ' ', $buffer);
}

ob_start('removeWhitespace');

//html source code

ob_get_flush();

with this code, my html source code will become one line(compress) but it's will break javascript code :

Before

<script type="text/javascript"><!--
$(document).ready(function() {
    $('.colorbox').colorbox({
        overlayClose: true,
        opacity: 0.5,
        rel: "colorbox"
    });
});
//--></script> 

After

<script type="text/javascript"><!-- $(document).ready(function() { $('.colorbox').colorbox({ overlayClose: true, opacity: 0.5, rel: "colorbox" }); }); //--></script>

then the javascript will not work anymore, so how to ignore if detect this <!-- or maybe just skip if detect javascript ?

Upvotes: 0

Views: 113

Answers (1)

V-tech
V-tech

Reputation: 174

Using negative look-behind:

preg_replace('/(?!<!--)\s+/', ' ', $buffer);

Upvotes: 2

Related Questions