Keddy1201 Keddy1201 - 2 years ago 117
PHP Question

php preg_match not working as in other web applications

Trying to parse the value of the following element:

input type="hidden" name="csrf_token"

Area around the element looks like this:

<input type="hidden" name="redirect" value="">
<input type="hidden" name="invite_code" value="">
<input type="hidden" name="invite" value="">
<input type="hidden" name="country" value="">
<input type="hidden" name="csrf_token" value="325fTt31vp42rbt90gNqBT923_Z04snI5tmhCOAhSzpmL2mr3NBAho1zp6bEirZrLdQna5Ocm6_iC3OYdbBqLg==">


With the following code line:

preg_match("/csrf_token. value=.(.+==).>/", $result, $output_array);

$output_array is left empty, where as in the regex is correct.

What am I doing wrong?

Answer Source

The Regex Way

Replace the literal space with \s+:

preg_match("/csrf_token.\s+value=.(.+==).>/", $result, $output_array);

Also, you probably want to improve a couple of other things here. Some suggestions:

  • Instead of . to match the ", use ['\"]? (less general, avoids matching on something random like csrf_token2).
  • Instead of .+==, use [^='"]+=* (same result, but much better performance, and matches tokens that end in 1 or zero =s).

With these suggestions, your code would be:

preg_match("/csrf_token['\"]?\s+value=['\"]?([^='\"]+=*)['\"]?>/", $result, $output_array);

Here's a working demo.

The Better Way

Don't parse HTML with regex, at all. Just use a parser.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download