dsfadsa08911 2010-03-01 00:59
浏览 63
已采纳

PHP:打印HTML友好代码?

I am wondering if there is any technique out there for making HTML code look good when printing that HTML (multiple lines) from a PHP function?

In particular, new lines and tabs. Obviously, I want the HTML tags behind-the-scenes to look good with the proper new lines and tabs. However, depending on when I call the PHP function that is printing the HTML, I might need to start the tabs at 1, 2, 3, 4, etc. I don't want to have to pass the starting tab count to the PHP function that is printing the HTML.

So, does anyone know a general-purpose way to print HTML-friendly code with PHP? Or is there a design principle that I am missing that says, "Don't print multiple HTML lines from a PHP function." ...

Thanks,

Steve

  • 写回答

5条回答 默认 最新

  • dongxun7301 2010-03-01 01:06
    关注

    Markup Cleanup

    There are some wonderful codetools out there that will cleanup (ie, adjust spacing) and repair (ie, close open tags).

    Tidy is one of the more popular html cleanup/repair tools. You can parse your script's output through Tidy on-the-fly using php's output buffers.

    Zend Dev Zone Intro - http://devzone.zend.com/article/761 Tidy Docs in PHP Manual - http://www.php.net/manual/en/book.tidy.php

    And now, a trivial example, yay!

    <?php
    ob_start();
    
    echo '<div><b> messy</b> <div> <i>blarg</I></div>';
    echo '  <div>code rawr!</div>';
    
    $output = ob_get_clean();
    
    $tidy = tidy_parse_string($output);
    tidy_clean_repair($tidy);
    echo $tidy;
    ?>
    

    Templating System

    You should be using a templating system to separate the logic (php code) and presentation (html/markup/etc) parts of your application.

    Using this practice has many advantages including, among other things, making your code easier to maintain and debug.

    This is a huge topic of discussion, and almost always goes into frameworks and MVC. There are thousands of resources, here are a few :P

    Writing a Template System in PHP - http://www.codewalkers.com/c/a/Display-Tutorials/Writing-a-Template-System-in-PHP/
    Separation of Business Logic from Presentation Logic in Web Applications - http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=21

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目