Making all PHP file output pass through a “filter file” before being displayed

后端 未结 5 1790
傲寒
傲寒 2020-12-17 02:43

Is there any way for all my PHP and/or HTML file output to be \"filtered\" before being displayed in the browser? I figured that I could pass it through a global function b

5条回答
  •  我在风中等你
    2020-12-17 03:07

    Check out ob_start which lets you pass a callback handler for post-processing your script output.

    For example, PHP includes a built-in callback ob_gzhandler for use in compressing the output:

    
    
    
    

    This should be a compressed page.

    Here's a fuller example illustrating how you might tidy your HTML with the tidy extension:

    function tidyhtml($input)
    {
        $config = array(
               'indent'         => true,
               'output-xhtml'   => true,
               'wrap'           => 200);
    
        $tidy = new tidy;
        $tidy->parseString($input, $config, 'utf8');
        $tidy->cleanRepair();
    
        // Output
        return $tidy;
    }
    
    ob_start("tidyhtml");
    
    //now output your ugly HTML
    

    If you wanted to ensure all your PHP scripts used the same filter without including it directly, check out the auto_prepend_file configuration directive.

提交回复
热议问题