Tidying PHP and HTML Code?

北城以北 提交于 2019-12-22 08:17:35

问题


I wonder if anyone could please help me I have been using HTML tidy and eclipses built-in function to tidy up my code. I am having great trouble with the following situations...

  1. when HTML is split between files via includes, having result structured with correct indentations helps with debugging via browser tools.

  2. PHP and HTML when used together. for example PHP if statements around HTML code where you wont the correct indentation for both the PHP and HTML. (automating this How to properly indent PHP/HTML mixed code?)

Situation one i can live with and there are ways around it. However, I would be grateful if anyone could offer a solution around situation two.

Tools I use eclipse 3.6, Aptanna 2.05 PDT 2.2


回答1:


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();

?>


来源:https://stackoverflow.com/questions/3585790/tidying-php-and-html-code

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