0

I'm trying to scan a form, and only pull out the fields that are not type="hidden" and retrieve their name="" value, I'm currently using

@<input.*?>@

Which retrieves the following for me,

(
[0] => Array
    (
        [0] => <input type="email" id="Contact0Email" name="email" class="field" onfocus="if ($(this).val() == $(this).attr('title')) { $(this).val('') }" onblur="if ($(this).val()=='') { $(this).val($(this).attr('title'));}" title="Enter a valid email here" value="Enter a valid email here">
        [1] => <input type="submit" class="submit" value="Get Instant Access">
    )

However I do not need all the code, I'd have to scan further to get what I need, anyone could suggest what regular expression I should use to get what I need? In this example there was no hidden fields, however there may be in some others I need to run this for.

Saulius Antanavicius
  • 1,371
  • 6
  • 25
  • 55

1 Answers1

0

Here is quick and dirty solution for you:

$string = '<form name="input" action="html_form_action.asp" method="get">
<input type="hidden" name="foo" value="123"/>
Username: <input type="text" name="user" value="Ralph">
Pass: <input type="text" name="pass">
<input type="submit" value="Submit">
</form>';

$doc = new DOMDocument();
$doc->loadHTML($string);

$input = $doc->getElementsByTagName('input');

for ($i = 0; $i < $input->length; $i++) {

    $el = $input->item($i);

    if ($el->getAttribute('type') === 'hidden'
      || $el->getAttribute('type') === 'submit') continue;

    echo $el->getAttribute('name')
        .':'.$el->getAttribute('value')."\n";

}
Roman Newaza
  • 11,405
  • 11
  • 58
  • 89