Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 文本框中的ob_get_内容和html字符串_Php_Html - Fatal编程技术网

Php 文本框中的ob_get_内容和html字符串

Php 文本框中的ob_get_内容和html字符串,php,html,Php,Html,这个问题很简单,但我似乎找不到解决办法 我正在使用以下命令从文件中获取html字符串: ob_start(); require_once 'view/form.php'; $html = ob_get_contents(); ob_end_clean(); 然后我用 echo '<br /><textarea style="width:100%" rows="10">'.$html.'</textarea>'; echo'.$html'; 但是html有

这个问题很简单,但我似乎找不到解决办法

我正在使用以下命令从文件中获取html字符串:

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

echo '<br /><textarea style="width:100%" rows="10">'.$html.'</textarea>';
echo'
.$html';
但是html有很多空白和制表符,非常混乱,有没有一种方法可以将其格式化为更具可读性的格式?

您可以使用(或任何其他html美化工具)美化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');
但请务必查看提供的链接,以查看更清晰、更全面的示例

这需要在安装php时安装tidy扩展。在Ubuntu上,您可以使用
apt get install php5-tidy
安装它。如果不可能安装扩展,请快速搜索“php html prettify”,我相信您将能够找到一个可以包含的Prettifyer类,例如


如果您想在文本框中显示HTML,请确保在回显之前先将其转义,这样在浏览器中它实际上不会被解释为HTML。

只需将其格式化为从缓冲区读取的生成HTML的任何文件,不要忘了使用
htmlspecialchars
转义HTML,否则,您将使输出无效,并可能中断页面。