开发者

php html parser performance

I need to extract a hidden input from a html document

<input type="hidden" name="email" id="email" value="email%4开发者_Python百科0hotmail.com">

I'm currently using http://simplehtmldom.sourceforge.net/ but I would like to know if there is any faster solution with lower RAM usage. The input is located somewhere at the middle of the document so is not necessary to load the entire html page . Would a regex work faster ? I will have to deal with millions of docs . To make it clear I need to extract only email%40hotmail.com


I find DomDocument with XPath pretty fast and good on memory. Another benefit is, that this is using defined standards, so pretty independent and accessible and normally anything needed to get the job done, so probably a bit more lightweight than loading a library that is using the same.

A simple string search (look for start pattern, look for end pattern) might be faster, but it does not scale well if the documents change. However this is even faster than compiling and running a regular expression.


If you need only exact matches to that format then sure, use a regex. You can't do general purpose html parsing with it, but you can get a simple pattern.

This will do it:

<input type="hidden" name="email" id="email" value="([^"]*)">

The wildcard matches anything that is not a double quote character. Don't forget to decode html entities.


You can make this:

$html = '<input type="hidden" name="email" id="email" value="email%40hotmail.com">';
preg_match('%<input type="hidden" name="email" id="email" value="([^\"]+)">%', $html, $email);

EDITED

I'm erred,regex is more fast.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜