ainoy31 Posted October 22, 2007 Share Posted October 22, 2007 Hello- I am trying to use preg_match() on a hidden input value, <input type = "hidden" name = "SESSIONID" value = "0000gecGfGKURryDNn4hgJ1nAFS:-1"> Keep in mind that the value will not be the same everytime. Here is the code I am having no luck with: preg_match('/<INPUT TYPE="HIDDEN" NAME="SESSIONID" VALUE="([^"]*?)" \/>/', $data, $matches); If I use print_r($matches), I receive an INVALID result. Is this possible to do? Basicly, I am trying to capture a hidden session id using the curl_setopt(). The session id value is auto generated. Much appreciation. Quote Link to comment Share on other sites More sharing options...
effigy Posted October 22, 2007 Share Posted October 22, 2007 You should use the /i modifier to make the pattern case insensitive. Since you've specified that you don't want to match a double quote, there's no need to make the character class match lazy: ([^"]*). Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted October 22, 2007 Author Share Posted October 22, 2007 Thanks for the replay back. What should I use if not ([^"]*?)? Quote Link to comment Share on other sites More sharing options...
effigy Posted October 22, 2007 Share Posted October 22, 2007 ([^"]*) Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted October 23, 2007 Author Share Posted October 23, 2007 OK. If the value I am trying to match up is 0000VzDZZt4xwA11b-6rngxzn6U:-1, why would ([A-Za-z0-9-:]+) not work since it will match letters, numbers, colon, and hyphens? Much appreciation for any suggestions. Thanks. Quote Link to comment Share on other sites More sharing options...
effigy Posted October 23, 2007 Share Posted October 23, 2007 It does work. Just a note: Always place the hyphen first in a character class since it can be a metacharacter for ranges. Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted October 23, 2007 Author Share Posted October 23, 2007 Here is what I have and I still get nothing in return when I echo it. preg_match('/<INPUT TYPE="HIDDEN" NAME="SESSIONID" VALUE="([-A-Za-z0-9:]+)">/', $data, $matches); The value is should something like 0000VzDZZt4xwA11b-6rngxzn6U:-1. Could it be that I am searching through a big file? The file has only one SESSIONID word. Quote Link to comment Share on other sites More sharing options...
effigy Posted October 23, 2007 Share Posted October 23, 2007 You still need the /i modifier: /pattern/i Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted October 23, 2007 Author Share Posted October 23, 2007 Added the /i modifier and still not luck. preg_match('/<INPUT TYPE="HIDDEN" NAME="SESSIONID" VALUE="([-A-Za-z0-9:]+)">/i', $data, $matches); Is my syntax wrong? Quote Link to comment Share on other sites More sharing options...
effigy Posted October 23, 2007 Share Posted October 23, 2007 If you want to match the exact data you posted, you need spaces around =: <?php preg_match('/<INPUT TYPE = "HIDDEN" NAME = "SESSIONID" VALUE = "([-A-Za-z0-9:]+)">/i', '<input type = "hidden" name = "SESSIONID" value = "0000gecGfGKURryDNn4hgJ1nAFS:-1">', $matches); print_r($matches); ?> Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted October 23, 2007 Author Share Posted October 23, 2007 Thanks for the advice but it is not working. I am trying to capture the SESSIONID value since it changes everything you go to the webpage. I am using the curl_setopt() method to accomplish this. $url = 'http://www.bullocks-express.com/Bullocks/entry'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt'); $data = curl_exec($ch); curl_close($ch); //(0000VzDZZt4xwA11b-6rngxzn6U:-1) -> example of the sessionid value preg_match('/<INPUT TYPE = "HIDDEN" NAME = "SESSIONID" VALUE = "([-A-Za-z0-9:]+)">/i', $data, $matches); print_r($matches); Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted October 23, 2007 Author Share Posted October 23, 2007 Thanks for all the help. I am going to research more on this. Will report a solution if I find one. Quote Link to comment Share on other sites More sharing options...
effigy Posted October 23, 2007 Share Posted October 23, 2007 What does the actual HTML look like? Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted October 23, 2007 Author Share Posted October 23, 2007 I believe I have narrow the issue down. The problem is from the website's url. If I use the http://www.bullocks-express.com/Bullocks/entry?nav=shipTrack or http://www.bullocks-express.com/Bullocks/entry? as my curl url, I do not always get the page where it has a form to fill out. The form has all the hidden fields and the SESSIONID value is one of them. That's why when I run this script, it comes up INVALID or an empty array. I found this out by replacing $data with '<input type = "hidden" name = "SESSIONID" value = "0000gecGfGKURryDNn4hgJ1nAFS:-1"> and running it. The regular expression returns 0000gecGfGKURryDNn4hgJ1nAFS:-1 as requested. I am going to contact their IT people. Thanks. Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted October 23, 2007 Author Share Posted October 23, 2007 The issue is from the website. The regular expression syntax does work. The site has a redirect on the .../Bullocks/entry page where the curl method can not follow. Thus, the hidden sessionid value is not found. Thank you for all the help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.