Load time: is it quicker to parse HTML with PHP's DOMDocument or with Regular Expressions?

耗尽温柔 提交于 2019-12-05 20:56:05

If you're willing to sacrifice speed for correctness, then go ahead and try to roll your own parser with regular expressions.

You say "Personally, I've found it more complicated to parse HTML through DOM." Are you optimizing for correctness of results, or how easy it is for you to write the code?

If all you want is speed and code that's not complicated, why not just use this:

$array_of_photos = Array( 'booger.jpg', 'aunt-martha-on-a-horse.png' );

or maybe just

$array_of_photos = Array();

Those run in constant time, and they're easy to understand. No problem, right?

What's that? You want accurate results? Then don't parse HTML with regular expressions.

Finally, when you're working with a parser like DOM, you're working with a piece of code that has been well-tested and debugged for years. When you're writing your own regular expressions to do the parsing, you're working with code that you're going to have to write, test and debug yourself. Why would you not want to work with the tools that many people have been using for many years? Do you think you can do a better job yourself on the fly?

I would use DOM as this is less likely to break if any small changes are made to the page.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!