Tidying PHP and HTML Code?

老子叫甜甜 提交于 2019-12-05 16:27:21

You could use HTML Tidy from within PHP to clean up your output. Use ob_start() and friends to get the whole HTML output as a string, then send it through Tidy. You might want to use som sort of caching if you do this, though.

<?php

    function callback($buffer)
    {
        // Clean up

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

        return tidy_repair_string($buffer, $config, 'utf8');
    }


    // Do some output.

    ob_start("callback");
    ?>
        <html>
            <body>
                <p>Outputting stuff here</p>
                <p>
                    Testing a broken tag:
                    <span> This span should be closed by Tidy.
                </p>
            </body>
        </html>
    <?php
    ob_end_flush();

?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!