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>
*/
Greg Guida
  • 7,302
  • 4
  • 30
  • 40
user1091856
  • 3,032
  • 6
  • 31
  • 42
  • I mean formatting the HTML source code. So it becomes more readable when looking at the source code. Like in the example. – user1091856 Dec 29 '11 at 07:12
  • dependent on your input in think, to use '\n' etc.. – jogesh_pi Dec 29 '11 at 07:13
  • unformatted:

    Hello World

    , 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...
    – user1091856 Dec 29 '11 at 07:15

3 Answers3

3

tidy. or one of the third party php libraries

2

This might help: Indent HTML Code

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
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;
Philip
  • 4,592
  • 2
  • 20
  • 28
  • 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. – user1091856 Dec 29 '11 at 07:41
  • 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. – Philip Dec 29 '11 at 07:58