1

I need to extract a hidden input from a html document

<input type="hidden" name="email" id="email" value="email%40hotmail.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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

I'm looking to get only the VALUE not the whole input . If I print the pattern you gave me I get [0] => Array ( [0] => <input type="hidden" name="email" id="email" value="example%40hotmail.com">
@Michael you add parens around the part you want to capture. I modified the answer for you.
If you want to quickly extract only the data similar to example, this is definitely the way to go. If you want to get correct value for unknown document (basically untrusted HTML) then you have to use full HTML5 parser these days because otherwise attacker that can affect the HTML contents may use comments or other tricks to get regex to match wrong part of the document.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.