整理PHP和HTML代码? [英] Tidying PHP and HTML Code?

查看:114
本文介绍了整理PHP和HTML代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以请帮助我我一直在使用HTML tidy和eclipses内置函数来整理我的代码。在以下情况下,我遇到了很大的麻烦...


  1. 当HTML通过包含在文件之间分割时,正确的缩进有助于通过浏览器工具进行调试。


  2. PHP和HTML一起使用时。例如PHP if语句围绕HTML代码,您不会在PHP和HTML中使用正确的缩进。 (自动执行此如何正确缩进PHP / HTML混合代码?


我可以忍受的一种情况,并有解决方法。然而,如果有人可以提供解决方案两种解决方案,我将不胜感激。

工具我使用eclipse 3.6,Aptanna 2.05 PDT 2.2

解决方案

您可以使用PHP中的HTML Tidy来清理输出。使用ob_start()和朋友将整个HTML输出作为字符串,然后通过Tidy发送。不过,如果你这样做的话,你可能想使用som类型的缓存。

 <?php 

函数回调($ buffer)
{
//清理

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

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


//做一些输出。

ob_start(callback);
?>
< html>
< body>
< p>输出东西< / p>
< p>
测试一个损坏的标签:
< span>这个范围应该由Tidy关闭。
< / p>
< / body>
< / html>
<?php
ob_end_flush();

?>


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

解决方案

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

?>

这篇关于整理PHP和HTML代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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