Jump to content

Hash Key?


shahrukh1

Recommended Posts

need to extract the hash value from this string

 

id="Qform__FormState" value="c49c31100c139a92a85a1dc37fb399c5"

 

proposed expression:

$expression = '#^([a-z0-9]{32})$#i';

 

does not work

 

tried this one as well

$expression = '#id=\"Qform__FormState\" value=\"^([a-z0-9]{32})$\"#i';

no luck either

Link to comment
Share on other sites

The most immediate reason why your regex is failing is because ^ and $ tell the regex engine to match the beginning and end of string. So in other words, you are telling the regex engine that the whole string must be that hash, nothing else. If you remove those anchors, you will get it to match. However... it will match any 32 alphanumeric substring within the content you are scraping. Ideally you should use a DOM parser like requinix suggested, but if you really must go the regex route, this will work and is fairly flexible:

 

$expression='#(id\s*=\s*["\']Qform__FormState["\'][^>]*)?([a-z0-9]{32})(?(1)|[^>]*id\s*=\s*["\']Qform__FormState["\'])#i';

 

This will allow for some format variation tolerances: random spacing, order of attributes, and quotes.

 

The matched hash will be in $match[2]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.