我正在尝试使用_fh
_nonce
和preg_match
的值
HTML
<form method="post" enctype="multipart/form-data" name="signin">
<input type="hidden" name="_fh" value="cd5a29509482534507a7a999ad0e0943">
<input type="hidden" name="_nonce" value="7fe456a0902ba89b365f760cdeb37113">
这是我的尝试
$returnValue = preg_match('#<input(.*)name="_fh"(.*)value="(.*?)"#is', '<form method="post" enctype="multipart/form-data" name="signin">
<input type="hidden" name="_fh" value="cd5a29509482534507a7a999ad0e0943">
<input type="hidden" name="_nonce" value="7fe456a0902ba89b365f760cdeb37113">
', $matches);
结果
array (
0 => '<input type="hidden" name="_fh" value="cd5a29509482534507a7a999ad0e0943">
<input type="hidden" name="_nonce" value="7fe456a0902ba89b365f760cdeb37113"',
1 => ' type="hidden" ',
2 => ' value="cd5a29509482534507a7a999ad0e0943">
<input type="hidden" name="_nonce" ',
3 => '7fe456a0902ba89b365f760cdeb37113',
)
答案 0 :(得分:2)
不要使用正则表达式来解析HTML。
建议使用HTML DOM parser,如下所示:
$html = <<<EOF
<form method="post" enctype="multipart/form-data" name="signin">
<input type="hidden" name="_fh" value="cd5a29509482534507a7a999ad0e0943">
<input type="hidden" name="_nonce" value="7fe456a0902ba89b365f760cdeb37113">
EOF;
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
#echo $doc->saveHTML();
$xpath = new DOMXPath($doc);
$val1 = $xpath->query('//input[@name="_fh"]/@value')->item(0)->nodeValue;
$val2 = $xpath->query('//input[@name="_nonce"]/@value')->item(0)->nodeValue;
echo $val1 . PHP_EOL;
echo $val2 . PHP_EOL;
<强>输出:强>
cd5a29509482534507a7a999ad0e0943
7fe456a0902ba89b365f760cdeb37113
答案 1 :(得分:1)
您可以使用preg_match_all
来获取键和值的嵌套数组。
$form = '<form method="post" enctype="multipart/form-data" name="signin">
<input type="hidden" name="_fh" value="cd5a29509482534507a7a999ad0e0943">
<input type="hidden" name="_nonce" value="7fe456a0902ba89b365f760cdeb37113">';
preg_match_all('# type="hidden" name="(.*?)" value="(.*?)"#is', $form, $matches);
print_r($matches);
收率:
Array
(
[0] => Array
(
[0] => type="hidden" name="_fh" value="cd5a29509482534507a7a999ad0e0943"
[1] => type="hidden" name="_nonce" value="7fe456a0902ba89b365f760cdeb37113"
)
[1] => Array
(
[0] => _fh
[1] => _nonce
)
[2] => Array
(
[0] => cd5a29509482534507a7a999ad0e0943
[1] => 7fe456a0902ba89b365f760cdeb37113
)
)
答案 2 :(得分:0)
如果您想获得所有隐藏输入的值。
$doc = new DOMDocument();
$doc->loadHTML('<form method="post" enctype="multipart/form-data" name="signin">
<input type="hidden" name="_fh" value="cd5a29509482534507a7a999ad0e0943">
<input type="hidden" name="_nonce" value="7fe456a0902ba89b365f760cdeb37113">');
$nodes = $doc->getElementsByTagName('input');
foreach ($nodes as $node) {
if ($node->hasAttributes()) {
foreach ($node->attributes as $attribute) {
if ($attribute->nodeName == 'type' && $attribute->nodeValue == 'hidden') {
$hidden_inputs[] = $node;
}
}
}
}
foreach ($hidden_inputs as $node) {
echo $node->getAttribute('value').'\n';
}
<强> DEMO 强>