Fixing invalid HTML in PHP?

I am creating a large HTML document from fragments provided by users who have an annoying habit of distorting various ways. Browsers are strong enough and forgiving, but I want to be able to check and (ideally) fix any invalid HTML, if at all possible. For example:

<td><b>Title</td> 

can reasonably be fixed on:

 <td><b>Title</b></td> 

Is there a way to make this easy in PHP?

+6
html php parsing
source share
3 answers

You can use HTML Tidy , man pages here .

+9
source share

I highly recommend the HTML cleaner . On your site:

HTML Cleaner is a standard compatible PHP filter library written in PHP. An HTML cleaner will not only remove all malicious code (better known as XSS) with a thoroughly tested, reliable whitelist, it will also make sure your documents are standards compliant, something only achievable with full W3C. Tired of using the BBCode because of the current landscape, inadequate or insecure HTML filters? Do you have a WYSIWYG editor, but he was never able to use it? Looking for high-quality, standards-compliant, open source components for this application that you create? HTML Cleaner for you!

+3
source share

If you cannot use Tidy (sometimes the hosting service does not activate this php module), you can use this PHP class: http://www.barattalo.it/html-fixer/

+1
source share

All Articles