0

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?

2
  • 1
    Just format it in whatever file is producing the html that you are reading from the buffer Commented Aug 6, 2014 at 9:45
  • 1
    And don't forget to use htmlspecialchars to escape your html, otherwise you will make your output invalid and might break your page. Commented Aug 6, 2014 at 9:51

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.