Jump to content


Photo

Making one class..


  • Please log in to reply
3 replies to this topic

#1 buceta

buceta
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 04 May 2006 - 10:54 PM

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

<?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;
    }
}

?>

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


#2 buceta

buceta
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 05 May 2006 - 01:21 PM

hints please guys

#3 lead2gold

lead2gold
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts
  • LocationOttawa, On

Posted 05 May 2006 - 02:45 PM

[!--quoteo(post=371394:date=May 4 2006, 06:54 PM:name=buceta)--][div class=\'quotetop\']QUOTE(buceta @ May 4 2006, 06:54 PM) View Post[/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:
preg_replace_callback($pattern,array($this,'cb_text'),$string);


#4 buceta

buceta
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 05 May 2006 - 05:12 PM

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users