问题
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...
when HTML is split between files via includes, having result structured with correct indentations helps with debugging via browser tools.
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
回答1:
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();
?>
来源:https://stackoverflow.com/questions/3585790/tidying-php-and-html-code