<p>I am looking for good methods of manipulating HTML in PHP. For example, the problem I currently have is dealing with malformed HTML.</p>

<p>I am getting input that looks something like this:</p>

<pre><code>&lt;div&gt;This is some &lt;b&gt;text
</code></pre>

<p>As you noticed, the HTML is missing closing tags. I could use regex or an XML Parser to solve this problem. However, it is likely that I will have to do other DOM manipulation in the future. I wonder if there are any good PHP libraries that handle DOM manipulation similar to how Javascript deals with DOM manipulation.</p>

[{'answer_id': 27236, 'author': 'ceejayoz', 'author_id': 1902010, 'author_profile': 'https://Stackoverflow.com/users/1902010', 'pm_score': 5, 'selected': True, 'text': '<p>PHP has <a href="http://php.net/tidy" rel="noreferrer">a PECL extension that gives you access to the features of HTML Tidy</a>.  Tidy is a pretty powerful library that should be able to take code like that and close tags in an intelligent manner.</p>\n\n<p>I use it to clean up malformed XML and HTML sent to me by a classified ad system prior to import.</p>\n'}, {'answer_id': 27284, 'author': 'Juan', 'author_id': 550, 'author_profile': 'https://Stackoverflow.com/users/550', 'pm_score': 1, 'selected': False, 'text': '<p>For manipulating the DOM i think that what you\'re looking for is <a href="http://www.php.net/manual/en/intro.dom.php" rel="nofollow noreferrer">this</a>. I\'ve used to parse HTML documents from the web and it worked fine for me.</p>\n'}, {'answer_id': 503956, 'author': 'bedlam', 'author_id': 45630, 'author_profile': 'https://Stackoverflow.com/users/45630', 'pm_score': 3, 'selected': False, 'text': '<p>I\'ve found PHP Simple HTML DOM to be the most useful and straight forward library yet. Better than PECL I would say.</p>\n\n<p>I\'ve written an article on <a href="http://www.crainbandy.com/programming/using-php-and-simple-html-dom-parser-to-scrape-artist-tour-dates-off-myspace" rel="noreferrer">how to use it to scrape myspace artist tour dates</a> (just an example.)  Here\'s a link to the <a href="http://simplehtmldom.sourceforge.net/" rel="noreferrer">php simple html dom parser.</a> </p>\n'}, {'answer_id': 4303405, 'author': 'Decko', 'author_id': 320702, 'author_profile': 'https://Stackoverflow.com/users/320702', 'pm_score': 2, 'selected': False, 'text': '<p>The DOM library which is now built-in can solve this problem easily. The loadHTML method will accept malformed XML while the load method will not.</p>\n\n<pre><code>$d = new DOMDocument;\n$d-&gt;loadHTML(\'&lt;div&gt;This is some &lt;b&gt;text\');\n$d-&gt;saveHTML();\n</code></pre>\n\n<p>The output will be:</p>\n\n<pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;\n&lt;html&gt;\n  &lt;body&gt;\n    &lt;div&gt;This is some &lt;b&gt;text&lt;/b&gt;&lt;/div&gt;\n  &lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/27222', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/889/']