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

Making all PHP file output pass through a quot;filter filequot; before being displayed(使所有 PHP 文件输出通过“过滤文件在显示之前)
本文介绍了使所有 PHP 文件输出通过“过滤文件"在显示之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

谢谢.

推荐答案

查看 ob_start 它可以让您通过用于后处理脚本输出的回调处理程序.

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

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

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>

这是一个更完整的示例,说明如何使用 tidy 扩展:

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

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

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 文件输出通过“过滤文件"在显示之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)