Get name via regex

0

How can I get the name of this input via regex ( preg_match_all )?

<input type="hidden" name="84a99a062288829a0f72271afea83ee9" value="_nc" />
    
asked by anonymous 05.06.2018 / 18:32

2 answers

1

I think the best way is to use DOMDocument of PHP, which is to work with HTML, for example:

$get = '<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<input type="hidden" name="84a99a062288829a0f72271afea83ee9" value="_nc" />
<input type="hidden" name="foo" value="bar" />
<input type="hidden" name="baz" value="boo" />
<input type="hidden" name="foobar" value="bazbar" />
</body>
</html>';

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

$allInputs = $doc->getElementsByTagName('input');
foreach ($allInputs as $input) {
    echo $input->getAttribute('name'), '<br>';
}

If you want to get only the element that has the value _nc , do an if:

$valor = null;    
$allInputs = $doc->getElementsByTagName('input');

foreach ($allInputs as $input) {
    if ($input->getAttribute('value') == '_nc') {
        $valor = $input->getAttribute('name'), '<br>';
    }
}

echo 'Valor do input:', $valor;

Or use DOMXpath :

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

$xpath = new DOMXpath($doc);

$valor = null;
$allInputs = $xpath->query("*/input[@value='_nc']");

foreach ($allInputs as $input) {
    $valor = $input->getAttribute('name'), '<br>';
}

echo 'Valor do input:', $valor;

Similar to what I replied to at link

    
05.06.2018 / 20:12
0

Use the match (?: name =) \ "(\ w *) \" and get the first group.

    
05.06.2018 / 19:31