Jump to content

Archived

This topic is now archived and is closed to further replies.

buceta

Making one class..

Recommended Posts

Hi, this is one try mine to make one class, the original content are simples functions.

[code]
<?php

class general_function
{
    function compress_html($output)
    {
        $this->output = $output; // output is the html code
        
        // init some variables/arrays
        $page_byte = strlen($this->output);
        $ascript   = array();
        $atext     = array();
        $apre      = array();
        
        // cut from output, and store away for later re-inclusion, everything between <textarea> and <pre> tags
        // (we do not want to touch those blocks as they may contain wanted whitespace)
        $this->output=preg_replace_callback('/<textarea.*?[^\btextarea>\b]*?textarea>/si', "cb_text", $this->output);
        $this->output=preg_replace_callback('/<pre.*?[^\bpre>\b]*?pre>/si', "cb_pre", $this->output);
        // also cut and save <nocompress> tagged text (intentional excluded parts)
        $this->output=preg_replace_callback('/<nocompress.*?[^\bnocompress>\b]*?nocompress>/si', "cb_text", $this->output);
        
        // strip all whitespace at the beginning of every new line (but leave the linebreak intact)
        $this->output=preg_replace('/(\r)?\n(\s*)/', "\n", $this->output);
        
        // cut from output, and store away for later re-inclusion, everthing between <script> tags
        // (we don't want to strip the linebreaks from script code since that would render the scripts non working)
        $this->output=preg_replace_callback('/<script.*?[^\bscript>\b]*?script>/si', "cb_script", $this->output);
        
        // strip all linebreaks and HTML-Comments from the output
        // (since we first strip all linebreaks, even multi-line comments will get striped this way! :-))
        $this->output = str_replace("\n" ," " ,$this->output );
        $this->output = preg_replace('/\/\*.*?[^\b\*\/\b]*?.*?\*\//',"" , $this->output);
        $this->output = preg_replace('/<!--[^\{\[\]*?[^b<!--\b]*?[^\b-->\b]*?-->/i', "", $this->output);
                                                   // We exclude HTML comments containing "{" and "["
                                                   // this way CSS definitions and AnyMedia stuff won't get striped
        
        // re-insert everything we saved before
        foreach ( $ascript as $block)
        {
            $this->output = str_replace( $block[1] , $block[0] , $this->output );
        }
        foreach ( $atext as $block)
        {
            $this->output = str_replace( $block[1] , $block[0] , $this->output );
        }
        foreach ( $apre as $block)
        {
            $this->output = str_replace( $block[1] , $block[0] , $this->output );
        }
        
        // last not least: strip any remaining multiple spaces between tags...
        $this->output = preg_replace('/>[\s]+</', "> <" ,$this->output );
        // ...and also strip the <nocompress> tags for keeping any XHTML compliance
        $this->output = str_replace("<nocompress>" ,"" ,$this->output );
        $this->output = str_replace("</nocompress>" ,"" ,$this->output );
        
        // calculate some nice looking numbers :-)
        $pagenew_byte       = strlen($this->output);
        $page_kilobyte      = number_format(($page_byte/1024),2);
        $pagenew_kilobyte   = number_format(($pagenew_byte/1024),2);
        $pagesaved_byte     = $page_byte - $pagenew_byte;
        $pagesaved_kilobyte = number_format((($pagesaved_byte)/1024),2);
        $pagesaved_perc     = number_format(((100*$pagesaved_byte)/$page_byte),2);
        
        $cmprstats = "<div align='center'><em>compress ratio: </em>$pagenew_kilobyte k/$page_kilobyte k (<strong>$pagesaved_perc%</strong>)</div>";
        
        $this->output = str_replace('</body>', $cmprstats . "</body>" , $this->output);
        
        // print the result or return?
        echo $this->output;
        //return $this->output;
    }
    
    // definition of callback functions
    function cb_script($matches)
    {
        global $ascript;
        
        $i = count($ascript) + 1;
        $h = md5($matches[0]);
          $ascript[$i][0] = $matches[0];
        $ascript[$i][1] = $h;
        return $h;
    }
    
    function cb_text($matches)
    {
        global $atext;
        
        $i = count($atext) + 1;
        $h = md5($matches[0]);
        $atext[$i][0] = $matches[0];
        $atext[$i][1] = $h;
        return $h;
    }
    
    function cb_pre($matches)
    {
        global $apre;
        $i = count($apre) + 1;
        $h = md5($matches[0]);
        $apre[$i][0] = $matches[0];
        $apre[$i][1] = $h;
        return $h;
    }
}

?>
[/code]

I always get this error:

Warning: preg_replace_callback() [function.preg-replace-callback]: requires argument 2, 'cb_text', to be a valid callback in .... line 17

Warning: preg_replace_callback() [function.preg-replace-callback]: requires argument 2, 'cb_pre', to be a valid callback in .... on line 18

Warning: preg_replace_callback() [function.preg-replace-callback]: requires argument 2, 'cb_text', to be a valid callback in .... on line 20

Warning: preg_replace_callback() [function.preg-replace-callback]: requires argument 2, 'cb_script', to be a valid callback in .... on line 27

How to fix this? What is wrong?

Sorry, I'm not a expert in php, but I want be one! lol

and please moderators, dont del this topic, just close it if the rules dont permit

Share this post


Link to post
Share on other sites
[!--quoteo(post=371394:date=May 4 2006, 06:54 PM:name=buceta)--][div class=\'quotetop\']QUOTE(buceta @ May 4 2006, 06:54 PM) [snapback]371394[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi, this is one try mine to make one class, the original content are simples functions.

I always get this error:

Warning: preg_replace_callback() [function.preg-replace-callback]: requires argument 2, 'cb_text', to be a valid callback in .... line 17

Warning: preg_replace_callback() [function.preg-replace-callback]: requires argument 2, 'cb_pre', to be a valid callback in .... on line 18

Warning: preg_replace_callback() [function.preg-replace-callback]: requires argument 2, 'cb_text', to be a valid callback in .... on line 20

Warning: preg_replace_callback() [function.preg-replace-callback]: requires argument 2, 'cb_script', to be a valid callback in .... on line 27

How to fix this? What is wrong?
[/quote]

Try this:
[code]
preg_replace_callback($pattern,array($this,'cb_text'),$string);
[/code]

Share this post


Link to post
Share on other sites
Thanks for attention.

Your change work fine, no more errors.

But the class dont work properly...

Always replace the script and <concompress> content with MD5 :~~~

I need change the function above, but what?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.