I wrote some code that'll reduce the size of the HTML in your documents and make it easier to download.
Include it at the top of your documents and call houtput() when you're done echoing stuff.
It's very slow though, and won't reduce loading time because it's so slow.
Since the code is big and I can't post it all at once, I'll post half now and the other half in the next post. Don't only use one half or it won't work...
PHP Code:
<?
//By Ilya.
//This file will reduce the size of your html.
//However, it won't reduce page loading time since it's so damn slow. It takes 5 seconds to optimize a 95k html file, but I got it down to 27k.
//So you'll probably have to make a cache, or optimize my code... You should be able to do a lot of that...
//Just include this file at the top of your code and call houtput() at the end of it.
//All arrays here should have consecutively numbered indexes starting at 0, I think.
//I just assume everything's correct with parameters and don't bother making error checks. If you screw up your html document, you'll screw up the optimizer.
//You must end img, br, ect tags with /> instead of just > or you'll confuse the optmizer.
//You can't use comments since I didn't bother implementing them... If you use comments, it'll screw up the optimizer.
//This uses output buffering, but you don't need it enabled since it enables it itself.
ob_start();
//Prefix used by an optimization. Set it to something that you wouldn't name any of your css classes.
//Ex. if it's abc, you'll have classes called abc_0, abc_1, abc_10285, ect, added to your code.
$html_cpref='abc';
//Keeps track of the time it takes to generate the page and echos it at the end as a comment.
$html_gtime=time()+microtime();
//The HTML of your document will be expanded into a tree, which is made up of two classes(cmem and tag). This is the first one.
//The content of the tag class is an array of these.
//All of the optimization functions are here, and the document is represented as an array of these also.
class html_cmem {
//Mode determines if val is a string or a tag. Could've used some function to do it but didn't since I'm used to C++....
//It's implicit that val is a string or a tag...
var $mode, $val; //mode: 0(string), 1(tag)
//This echos the contents of val.
function echoc() {
if ($this->mode==0) return $this->val; else return $this->val->echoc();
}
//This removes extra spacing from val if it's a string. All spacing will only have one spacing charecter(ex. " " becomes " " ;;; "\n " becomes "\n" ;;; " \n" becomes " ")
//Otherwise, it'll call itself on all of val's content, which would either be strings or would call it on more content.
//For tags, it'll also remove extra spacing from tag parameters, from every one specified in $tpars. $tpars should be an array of strings.
//Spacing charecters are " ", "\n", "\r", and "\t".
function clearsp(&$tpars) {
if ($this->mode) {
for ($x=0;$x<sizeof($this->val->content);$x++) $this->val->content[$x]->clearsp($tpars);
for ($x=0;$x<sizeof($tpars);$x++) {
$cid=$this->val->pindex($tpars[$x]);
if ($cid==-1) continue;
$cpos=0; $sc=0; $res='';
while ($cpos<strlen($this->val->param[$cid][1])) {
$cchar=$this->val->param[$cid][1][$cpos];
$csp=$cchar==' ' || $cchar=="\n" || $cchar=="\r" || $cchar=="\t";
if ($csp) {
$sc++;
if ($sc<2) $res.=$cchar;
} else {
$sc=0;
$res.=$cchar;
}
$cpos++;
}
$this->val->param[$cid][1]=$res;
}
} else {
$cpos=0; $sc=0; $res='';
while ($cpos<strlen($this->val)) {
$cchar=$this->val[$cpos];
$csp=$cchar==' ' || $cchar=="\n" || $cchar=="\r" || $cchar=="\t";
if ($csp) {
$sc++;
if ($sc<2) $res.=$cchar;
} else {
$sc=0;
$res.=$cchar;
}
$cpos++;
}
$this->val=$res;
}
}
//This will look for style tags and replace them with class tags.
//$tcss is an array of strings, and the strings are the contents of all of the style tags the function found.
//There won't be any duplicate tags in $tcss but the function doesn't ignore spacing.
//$tcss should be empty when you first call the function. The function will fill it up with data.
//Use the $tcss you used the first time on any consecutive calls in the same document.
//This and the next function use the prefix I talked about earlier to name the classes.
//They're also slow.
function cssopt(&$tcss) {
global $html_cpref;
if (!$this->mode) return;
for ($x=0;$x<sizeof($this->val->content);$x++) $this->val->content[$x]->cssopt($tcss);
$csty=$this->val->pindex('style'); if ($csty==-1) return;
$ccls=$this->val->pindex('class');
$aai=-1;
for ($x=0;$x<sizeof($tcss);$x++) if ($tcss[$x]==$this->val->param[$csty][1]) { $aai=$x; break; }
if ($aai==-1) {
$aai=sizeof($tcss);
$tcss[$aai]=$this->val->param[$csty][1];
}
for ($x=$csty;$x<sizeof($this->val->param)-2;$x++) $this->val->param[$x]=$this->val->param[$x+1];
unset($this->val->param[sizeof($this->val->param)-1]);
if ($ccls==-1) {
$ccls=sizeof($this->val->param);
$this->val->param[$ccls][0]='class';
$this->val->param[$ccls][1]=$html_cpref .'_'. $aai;
} else $this->val->param[$ccls][1].=' '. $html_cpref .'_'. $aai;
}
//This writes the style tags you get with cssopt to the document as a <style> element in the head.
//If it's called on an html tag, it'll look for a head tag.
//If it's called on a head tag, it'll add the style element to the head.
//If it's not called on either, it won't do anything.
//If you have more than one html or head tag in your document for some reason, there will be multiple style elements added and they'll waste space assuming your document works in the first place.
//If you don't have a head tag inside of an html tag, or the html tag is inside another tag, nothing will happen and all of your styles will go away.
//If ...
function csswrite(&$tcss) {
global $html_cpref;
if (!$this->mode) return;
if ($this->val->tag=='html') for ($x=0;$x<sizeof($this->val->content);$x++) $this->val->content[$x]->csswrite($tcss);
if ($this->val->tag=='head') {
$ctag=new html_tag;
$ctag->tag='style';
$ctag->param=array(
0 => array(
0 => 'type',
1 => 'text/css'
)
);
$ctag->mode=1;
$ctag->content=array(new html_cmem); $ctag->content[0]->mode=0; $ctag->content[0]->val='';
for ($x=0;$x<sizeof($tcss);$x++) $ctag->content[0]->val.='.'. $html_cpref .'_'. $x .' {'. $tcss[$x] .'}';
$this->val->content[sizeof($this->val->content)]=$ctag;
}
}
}
//This is an html tag.
//It can be one tag that closes itself, two tags with some content in between, a comment, or a special tag...
//Actually, it can only be one or two tags since I didn't implement the rest since I don't use them.
class html_tag {
var $tag, $param, $content, $mode; //mode: 0(one tag), 1(two tags), 2(comment), 3(special tag; not used yet)
//Echos the tag.
function echoc() {
$res='';
if ($this->mode==0) {
$res.='<'. $this->tag .' ';
for ($x=0;$x<sizeof($this->param);$x++) $res.=$this->param[$x][0] .'="'. $this->param[$x][1] .'" ';
$res.='/>';
return $res;
}
if ($this->mode==1) {
$res.='<'. $this->tag .' ';
for ($x=0;$x<sizeof($this->param);$x++) $res.=$this->param[$x][0] .'="'. $this->param[$x][1] .'" ';
$res.='>';
for ($x=0;$x<sizeof($this->content);$x++) $res.=$this->content[$x]->echoc();
$res.='</'. $this->tag .'>';
return $res;
}
if ($this->mode==2) {
$res.='<!--';
for ($x=0;$x<sizeof($this->content);$x++) $res.=$this->content[$x]->echoc();
$res.='-->';
return $res;
}
return 'oh ************!';
}
//Gets the location of a parameter with a name of $targ in the $param array.
function pindex($targ) {
for ($x=0;$x<sizeof($this->param);$x++) if ($this->param[$x][0]==$targ) return $x;
return -1;
}
};