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.

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

报告相同问题?

悬赏问题

  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决