PHP field value preg_match

I want to get the identifier or name of a field that continues to change its value.

Example:

I have these fields in my form:

<input type="text" id="94245" name="94245" value="">
<input type="text" id="name" name="name" value="">
<input type="text" id="status" name="status" value="">

And I want to get the identifier or name of the first field, but it continues to change, for example:

<input type="text" id="52644" name="52644" value="">
<input type="text" id="44231" name="44231" value="">
<input type="text" id="94831" name="94831" value="">

A pattern is always a 5-digit value.

I tried this but did not succeed:

preg_match('/(\d)id=\s*([0-9]{5})\s*(\d+)/', $html, $fieldId);
+4
source share
3 answers

Try the following:

preg_match('/\bid\s*=\s*"(\d{5})"/', $html, $fieldId);
  • The escape sequence for the word boundary \b, not \d.
  • There is no reason to use a gripping group around \bsince it corresponds to zero length.
  • You did not match double quotes around the identifier.
  • You can use \dinstead [0-9]to match numbers.
  • \d+, , .
+2

, . . , :

if (preg_match('/\\s*name\\s*=\\s*"([0-9]{5})"/', $html, $gregs)) {
    $fieldId = $gregs[1];
}

, id name. , , name.

, , "] * > " ( 't HTML ), array_map, .

+2

You can use brute force. The following numeric_name1.php file submits a form in which the first field has a numeric name. The second numeric_name2.php file iterates through all the numbers until it finds the one that exists in $ _POST. Here they are:

numeric_name1.php

<html>
  <body>
    <form method="post" action="numeric_name2.php">
      <input type="text" id="94245" name="94245" value="" />
      <input type="text" id="name" name="name" value="" />
      <input type="text" id="status" name="status" value="" />
      <input type="submit" value="Submit this form"/>
    </form>
  </body>
</html>

numeric_name2.php

<?php
for ( $i = 0; $i < 99999; $i++ )      // TRY ALL NUMBERS WITH 5 DIGITS.
{ $num = sprintf( "%05d",$i );        // EXPAND $I TO A STRING WITH 5 DIGITS.
  if ( isSet( $_POST[ $num ] ) )      // IF THIS NUMERIC NAME EXISTS...
     { echo "The name is = " . $num;
       break;                         // STOP WHEN FOUND.
     }
}
?>

To check this code, create two text files, use the specified names, copy the code, open your browser and run "localhost / numeric_name1.php".

+2
source

All Articles