在显示之前,使所有PHP文件输出通过"过滤器文件"

Gau*_*rma 5 php filter

有没有办法让我的所有PHP和/或HTML文件输出在浏览器中显示之前被"过滤"?我想我可以在它显示之前通过一个全局函数传递它,但我坚持实现.请帮忙.

如果有更好的方法来达到相同的结果,我会很高兴知道.

谢谢.

Pau*_*xon 14

查看ob_start,它允许您传递回调处理程序以对脚本输出进行后处理.

例如,PHP包含一个内置回调ob_gzhandler,用于压缩输出:

<?php

ob_start("ob_gzhandler");

?>
<html>
<body>
<p>This should be a compressed page.</p>
</html>
<body>
Run Code Online (Sandbox Code Playgroud)

这是一个更全面的示例,说明如何使用整洁的扩展来整理HTML :

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
Run Code Online (Sandbox Code Playgroud)

如果要确保所有PHP脚本都使用相同的过滤器而不直接包含它,请查看auto_prepend_file配置指令.