Preg_match表单未隐藏的字段,仅检索名称

时间:2013-03-27 01:45:58

标签: php regex

我正在尝试扫描表单,只拔出不是type =“hidden”的字段并检索其名称=“”值,我目前正在使用

@<input.*?>@

为我检索以下内容,

(
[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">
    )

但是我不需要所有的代码,我必须进一步扫描以获得我需要的东西,任何人都可以建议我应该用什么正则表达式来获得我需要的东西?在这个例子中没有隐藏的字段,但是在其他一些我可能需要运行它。

1 个答案:

答案 0 :(得分:0)

以下是快速而肮脏的解决方案:

$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";

}
相关问题