PHP preg_match specific field value

女生的网名这么多〃 提交于 2019-12-10 17:28:29

问题


I want to get the id or the name of a field that keeps changing 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 ID or the name of the first field, but it keeps changing, like:

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

The pattern is that is always a 5 number value.

I've tried this, but no success:

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

回答1:


Try this:

preg_match('/\bid\s*=\s*"(\d{5})"/', $html, $fieldId);
  • The escape sequence for a word boundary is \b, not \d.
  • There's no reason to use a capture group around \b, since it's a zero-length match.
  • You didn't match the double quotes around the ID.
  • You can use \d instead of [0-9] to match digits.
  • There's no \d+ after the ID, I don't know what that's for in your regexp.



回答2:


You appear to simply have omitted the quotation marks. Otherwise, your regex is sound. You may want to place \s's around the equal sign, just in case:

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

As you can see I have also changed the searched attribute from id to name. In your example they're equal, but the value being sent will be the one specified by name.

If this name-rewriting anti-scraping strategy is ever applied to more fields, you can first get an array of fields by looking for "]*>" (even though you shouldn't parse HTML with regex), and then use array_map to extract each of them to a string with the tag name.




回答3:


You can use brute force. Next file, numeric_name1.php, sends the form, where the first field has a numeric name. The second file, numeric_name2.php, iterates all numbers until it finds 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 test this code, create two text files, use the given names, copy-paste the code, open your browser and run "localhost/numeric_name1.php".



来源:https://stackoverflow.com/questions/30790885/php-preg-match-specific-field-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!