Welcome to WebmasterWorld Guest from 54.146.139.201

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

crunch up your HTML into a single line

a handy little script...

     
8:29 pm on Jun 7, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Feb 12, 2006
posts:2564
votes: 50


this won't be everyone's cup of tea, but i found it pretty handy when someone gave it to me so i thought i'd pass it on.

it's a little script to remove all the line breaks, spaces and gaps between tags. if you stick it around your HTML page then you can write your code with as many spaces and gaps as you want to keep it easy to read, but when it's delivered to the user it will appear as one big line. maybe save a bit of bandwidth.

(it doesn't crunch up anything inside <script> </script> though, because that will frequently break the script)


<?php

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){$bff=str_replace(array("\r\r\r","\r\r","\r\n","\n\r","\n\n\n","\n\n"),"\n",$bff);
$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<d",">\n<d"),"><d",$bff);$bff=str_replace(array(">\r<h",">\n<h"),"><h",$bff);
$bff=str_replace(array(">\r<i",">\n<i"),"><i",$bff);$bff=str_replace(array(">\r<i",">\n<i"),"><i",$bff);
$bff=str_replace(array(">\r<l",">\n<l"),"><l",$bff);$bff=str_replace(array(">\r<m",">\n<m"),"><m",$bff);
$bff=str_replace(array(">\r<p",">\n<p"),"><p",$bff);$bff=str_replace(array(">\r<t",">\n<t"),"><t",$bff);
$bff=str_replace(array(">\r</u",">\n</u"),"></u",$bff);$bff=str_replace(array(">\r</d",">\n</d"),"></d",$bff);
$bff=str_replace(array(">\r<!",">\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"),"/>",$bff);
$bff=ereg_replace(" {2,}",' ',$bff);$bff=str_replace("> <","><",$bff);$bff=str_replace(" &nbsp;","&nbsp;",$bff);
$bff=str_replace("&nbsp; ","&nbsp;",$bff);return $bff;}ob_start("stripwhitespace");

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<title>Title of your page</title>

</head>
<body>

<h1>Name of the page</h1>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean porttitor. Curabitur eros ante, varius sit amet, laoreet at, vehicula dictum, ligula. Cras sed lorem. Aenean dignissim rutrum massa. Aliquam tortor. Vivamus condimentum. Nam mollis ligula sed nisi. Phasellus posuere orci et elit. Nam dapibus bibendum nibh. Curabitur at arcu. Integer pretium. In lobortis feugiat est.</p>

<p>Donec sed est at dolor rhoncus ornare. Nam pellentesque tortor sed diam. Quisque nec odio. Morbi nibh. Sed risus nisi, tincidunt eget, consectetuer id, imperdiet porta, purus. Donec molestie, felis a mollis volutpat, pede lectus semper diam, sed pretium purus tellus nec risus. Proin sagittis ante quis sem. Cras semper magna mollis arcu.</p>

</body>
</html><?php
ob_end_flush();
?>

9:25 pm on June 7, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


Thanks for the script, londrum.

For those interested this uses the output control functions [php.net]. These are very handy and sometimes necessary, especially when dealing with echoing strings within a loop.

Also, there is a function called php_strip_whitespace [php.net]. I'm sure you can figure out what it does ;)