user2650277
user2650277

Reputation: 6739

Getting hidden input values with preg_match

I am trying to get the value of _fh and _nonce with 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">

Here is my attempt

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

Result

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',
)

Upvotes: 2

Views: 1934

Answers (3)

Manh Nguyen
Manh Nguyen

Reputation: 940

If you want to get value of all hidden inputs.

$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

Upvotes: 0

anubhava
anubhava

Reputation: 785531

Don't use regex for parsing HTML.

Recommended is to use HTML DOM parser like this:

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

Output:

cd5a29509482534507a7a999ad0e0943
7fe456a0902ba89b365f760cdeb37113

Upvotes: 2

Tim
Tim

Reputation: 2187

You can use preg_match_all to get nested arrays of the keys and values.

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

Yields:

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
        )

)

Upvotes: 1

Related Questions