doom910730 2014-08-06 09:39
浏览 68
已采纳

文本框中的ob_get_contents和html字符串

Question is simply easy, but it seems I can't find solution so..

I'm getting html string from file using:

ob_start();
require_once 'view/form.php';
$html = ob_get_contents();
ob_end_clean();

And it works, then I'm putting this string in text box using

echo '<br /><textarea style="width:100%" rows="10">'.$html.'</textarea>';

But html goes out messy with lots of white spaces and tabs, is there a way to format it in decent way to be more readable?

  • 写回答

1条回答 默认 最新

  • dqwh1209 2014-08-06 09:53
    关注

    You can use Tidy (or any other HTML prettifier) to prettify HTML.

    ob_start();
    require_once 'view/form.php';
    $html = ob_get_contents();
    ob_end_clean();
    
    $config = array('indent' => TRUE, 'output-xhtml' => TRUE); 
    $prettyhtml = tidy_parse_string($html, $config, 'UTF8');
    

    But please do take a look at the link provided, to see a cleaner, more thorough example.

    This requires the tidy extension to be installed with your php install. On Ubuntu, you can install it using apt-get install php5-tidy. If installing extensions is not a possibility, do a quick search for 'php html prettify', and I'm sure you'll be able to find a prettifier class you can include, such as htmLawed.

    If you want to display the HTML in your textbox, make sure you escape it before echoing it, so it's not actually interpreted as HTML in the browser.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向