使所有的PHP文件输出都通过一个“过滤器文件”在显示之前 [英] Making all PHP file output pass through a "filter file" before being displayed

查看:150
本文介绍了使所有的PHP文件输出都通过一个“过滤器文件”在显示之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



如果有更好的方法来达到同样的结果,我很乐意知道。



谢谢。 nofollow noreferrer> ob_start ,它可以让你传递一个回调处理器来处理你的脚本输出。


$ b 例如,PHP包含一个内置的回调<一个用于压缩输出的href =http://php.net/ob_gzhandler =nofollow noreferrer> ob_gzhandler :

 <?php 

ob_start(ob_gzhandler);

?>
< html>
< body>
< p>这应该是一个压缩页面。< / p>
< / html>
< body>

下面是一个更完整的例子,说明如何使用 tidy extension

  function tidyhtml($ input)
{
$ config = array(
'indent'=> true,
'output-xhtml'=> true,
'wrap'=> 200);

$ tidy = new tidy;
$ tidy-> parseString($ input,$ config,'utf8');
$ tidy-> cleanRepair();

//输出
return $ tidy;
}

ob_start(tidyhtml);

//现在输出丑陋的HTML

如果您想确保所有你的PHP脚本使用相同的过滤器,而不直接包括它,检查 auto_prepend_file 配置指令。


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 before it is displayed but I'm stuck on the implementation. Please help.

If there is a better way to achieve the same result, I'd be happy to know.

Thanks.

解决方案

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:

<?php

ob_start("ob_gzhandler");

?>
<html>
<body>
<p>This should be a compressed page.</p>
</html>
<body>

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.

这篇关于使所有的PHP文件输出都通过一个“过滤器文件”在显示之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆