Personal Development for Smart People Forums

Personal Development for Smart PeopleTM Forums


Go Back   Personal Development for Smart People Forums > Personal Development > Technology & Technical Skills
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Technology & Technical Skills Computer skills, hardware, software, internet topics, gadgets, programming


Welcome to the Personal Development for Smart People Forums, the place for lively, intelligent discussion of all personal growth issues -- physical, mental, financial, social, emotional, spiritual, and more.

You're currently viewing as a guest, which gives you limited read-only access. By joining our free community, you'll be able to post your own messages, access many members-only features, see the new messages posted since your last visit, and of course remove this header message. Registration is fast, simple, and free, so please join today.

If you arrived here from a search engine, you may want to explore the main site first, which includes hundreds of deep and insightful articles on a variety of personal development topics.

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 05-04-2007, 03:07 AM
Senior Member
 
Join Date: Nov 2006
Posts: 316
Minsc is on a distinguished road
Default PHP Code that optimizes your HTML

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(
                    
=> array(
                        
=> 'type',
                        
=> '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;
        }
    };
__________________
There is nothing on sundersoft.com.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-04-2007, 03:08 AM
Senior Member
 
Join Date: Nov 2006
Posts: 316
Minsc is on a distinguished road
Default

Here's the other half:

PHP Code:
    //This large, recursive function converts normal old html into the classes above.
    //It's very slow.
    
function hproc($query) {
        
$res=array(); $resm=array();
        
$scope=0$buffer=''$cmode=0$cloc=0;
        
$lchar=''$cchar='';
        while (
$cloc<strlen($query)) {
            
$lchar=$cchar;
            
$cchar=$query[$cloc];
            
$scend=0$scmode=0;
            if (
$scope==0) {
                if (
$cmode==0) { if ($cchar=='<') {
                    
$cmode=1;
                    if (
strlen($buffer)>0) {
                        
$res[sizeof($res)]=$buffer;
                        
$resm[sizeof($resm)]=0;
                        
$buffer='';
                    }
                }} else if (
$cchar=='>') {
                    
$cmode=0;
                    if (
$lchar=='/') {
                        
$scend=1;
                        
$scmode=2;
                    } else 
$scope=1;
                }
            } else {
                if (
$cmode==0) { if ($lchar=='<') {
                    if (
$cchar=='/'$cmode=2; else $cmode=1;
                }} else if (
$cmode==1) { if ($cchar=='>') {
                    
$cmode=0;
                    if (
$lchar!='/'$scope++;    
                }} else if (
$cmode==&& $cchar=='>') { $cmode=0$scope--; }
                if (
$scope==0) { $scend=1$scmode=1; }
            }
            
$buffer.=$cchar;
            if (
$scend || $cloc==strlen($query)-1) {
                
$res[sizeof($res)]=$buffer;
                
$resm[sizeof($resm)]=$scmode;
                
$buffer='';
                
$scend=0;
            }
            
$cloc++;
        }
        
//
        
$rres=array();
        for (
$cx=0;$cx<sizeof($res);$cx++) {
            
$rres[$cx]=new html_cmem;
            if (
$resm[$cx]==0) { $rres[$cx]->mode=0$rres[$cx]->val=$res[$cx]; } else {
                
$cpos=1;
                
$cmode=0;
                
$title='';
                
$params=array(); $cparam=0$pmode=0$pside=0$newp=1;
                while (
$res[$cx][$cpos]!='>') {
                    
$cchar=$res[$cx][$cpos];
                    if (
$cchar=='/' && $res[$cx][$cpos+1]=='>') break;
                    
$sep=$cchar==' ' || $cchar=="\n" || $cchar=="\r" || $cchar=="\t";
                    if (
$cmode==0) {
                        if (
$sep) {
                            if (
strlen($title)!=0$cmode=1;
                        } else 
$title.=$cchar;
                    } else if (
$cmode==1) {
                        if (
$newp && !$sep) {
                            
$params[$cparam][0]='';
                            
$params[$cparam][1]='';
                            
$newp=0;
                        }
                        if (
$pside==1) {
                            if (
$pmode==&& $cchar=='"'$pmode=1; else {
                                if (
$cchar=='"') {
                                    
$pmode=0;
                                    
$pside=0;
                                    
$cparam++;
                                    
$newp=1;
                                }
                                else 
$params[$cparam][1].=$cchar;
                            }
                        } else if (!
$sep) {
                            if (
$cchar=='=') {
                                
$pside=1;
                            } else 
$params[$cparam][0].=$cchar;
                        }
                    }
                    
$cpos++;
                }
                if (
$resm[$cx]==1) {
                    
$epos=strlen($res[$cx])-1;
                    while (
$res[$cx][$epos]!='<'$epos--;
                    
$content=substr($res[$cx], $cpos+1, ($epos-$cpos)-1);
                    
$content=hproc($content); //woohoo
                
} else $content=array();
                
$rres[$cx]->mode=1;
                
$rres[$cx]->val=new html_tag;
                
$rres[$cx]->val->mode=$resm[$cx]==2?0:1;
                
$rres[$cx]->val->param=$params;
                
$rres[$cx]->val->content=$content;
                
$rres[$cx]->val->tag=$title;
                
/*echo '<!--'. $title .' PARAMS: ';
                for ($x=0;$x<sizeof($params);$x++) echo '@'. $params[$x][0] .'@=@'. $params[$x][1] .'@  ';
                echo ' @@ '. $content .' @@';
                echo '-->';*/
            
}
        }
        return 
$rres;
    }
    
    
//This is the function that you're supposed to call once you're done echoing stuff.
    
function houtput() {
        global 
$html_gtime;
        
$code=ob_get_contents();
        
ob_end_clean();
        
$pcode=hproc($code);
        
//The first is the array used to remove spacing. Spacing is removed from parameters of tags if the parameters are listed here.
        //You should leave the second alone.
        
$tpars=array('style''div'); $tcss=array();
        for (
$x=0;$x<sizeof($pcode);$x++) {
            
$pcode[$x]->clearsp($tpars); //Removes spacing.
            
$pcode[$x]->cssopt($tcss); //Removes style tags.
        
}
        for (
$x=0;$x<sizeof($pcode);$x++) $pcode[$x]->csswrite($tcss); //Writes the classes that the style tags were replaced with.
        
for ($x=0;$x<sizeof($pcode);$x++) echo $pcode[$x]->echoc(); //Glues the optimized document together and gives it to the browser.
        
$html_gtime=(time()+microtime())-$html_gtime;
        echo 
'<!--GENERATED IN '$html_gtime .' SECONDS.-->'//How long everything took.
    
}
?> 
__________________
There is nothing on sundersoft.com.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!