开发者

Html Parser for PHP like Java

开发者 https://www.devze.com 2022-12-20 22:21 出处:网络
I have been developing Java programs that parse html source code of webpag开发者_高级运维es by using various html parsers like Jericho, NekoHtml etc...

I have been developing Java programs that parse html source code of webpag开发者_高级运维es by using various html parsers like Jericho, NekoHtml etc...

Now I want to develop parsers in PHP language. So before starting, I want to know that are there any html parsers available that I can use with PHP to parse html code


Check out DOMDocument.

Example #1 Creating a Document

<?php
$doc = new DOMDocument();
$doc->loadHTML("<html><body>Test<br></body></html>");
echo $doc->saveHTML();


The builtin class DOM parser does a very good job. There are many other xml parsers, too.


DOM is pretty good for this. It can also deal with invalid markup, however, it will throw undocumented errors and exceptions in cases of imperfect markup so I suggest you filter HTML with HTMLPurifier or some other library before loading it with the DOM.

0

精彩评论

暂无评论...
验证码 换一张
取 消