Reputation: 1522
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
Reputation: 174
Using negative look-behind:
preg_replace('/(?!<!--)\s+/', ' ', $buffer);
Upvotes: 2