Keddy1201 Keddy1201 - 5 months ago 21
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"
value="VUNht8fnmxmJXJIMassWW8SAwWKNJ3SC8POA4FtSqEKhG1rcoB3ZNqcPqa615tPsF_hzW0l4zDjSEHJYMz9Ogw==">


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==">

<div>


With the following code line:

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


$output_array is left empty, where as in http://www.phpliveregex.com/ the regex is correct.

What am I doing wrong?

Answer

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.