問題描述
有沒有辦法讓我的所有 PHP 和/或 HTML 文件輸出在顯示在瀏覽器中之前被過濾"?我想我可以在它顯示之前通過一個全局函數(shù)傳遞它,但我被困在實現(xiàn)上.請幫忙.
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.
如果有更好的方法來實現(xiàn)相同的結(jié)果,我很樂意知道.
If there is a better way to achieve the same result, I'd be happy to know.
謝謝.
推薦答案
查看 ob_start 它可以讓您通過用于后處理腳本輸出的回調(diào)處理程序.
Check out ob_start which lets you pass a callback handler for post-processing your script output.
例如,PHP 包含一個內(nèi)置回調(diào) 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.
這篇關(guān)于使所有 PHP 文件輸出通過“過濾文件"在顯示之前的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!