8

Is there a PHP function/class that cleans my HTML code?

For example:

$html = "<ul><li>item1</li><li>item2</li>";
echo htmlClean($html);

/*
    Outputs:
    <ul>
        <li>item1</li>
        <li>item2</li>
    </ul>
*/
3
  • I mean formatting the HTML source code. So it becomes more readable when looking at the source code. Like in the example. Commented Dec 29, 2011 at 7:12
  • dependent on your input in think, to use '\n' etc.. Commented Dec 29, 2011 at 7:13
  • unformatted: <div><p>Hello World</p></div>, then the function returns the given html with line breaks and tabbed spaces according to element depth... i'm afraid i'm not using the right term... Commented Dec 29, 2011 at 7:15

3 Answers 3

3

tidy. or one of the third party php libraries

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

Comments

2

This might help: Indent HTML Code

1 Comment

@DemoUser your link is broken
0

keep html out of your php for a start, you should only echo out strings not blocks of html.

<ul>
      <?php foreach($items as $item): ?>
        <li><?php echo $item['one']; ?></li>
      <?php endforeach; ?>
</ul>

If needs must you could try clean the code up with " Heredoc string quoting "

echo <<<EOF
<ul>
<li>"$item1"</li>
<li>"$item2"</li>
</ul>
EOF;

2 Comments

In this case is hard to keep HTML from PHP, I have a function that returns an HTML UL list based on the categories stored on MySQL. In this case, php does all the hard work of making the list, I just have to pass the table name where the categories names are stored.
Your output does not suggest this...may we seen the full code. Im sure we can give you a better suggestion if we see what your trying to achieve mate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.