marklarah Posted November 15, 2009 Share Posted November 15, 2009 Hi! So I'm scraping a page, and the following text comes up, from which a string needs extracting/ <a class="nav" title="|Bar 24" href="index.php?page=session=g7657h7h575&token=[NEEDTHISSTRING]"> The session=g7657h7h575, I already will have the session key stored in a variable, (but not the 24, the number could be a one or two digit number.) Anyway, I've been pulling my hair out with preg_matches and m tokens and (.+?) and stuff...nothing seems to work. Anyone want to guide me in the right direction? Thanks.... Quote Link to comment Share on other sites More sharing options...
marklarah Posted November 15, 2009 Author Share Posted November 15, 2009 Ie, can anyone help me set up a regex string to find the token? I assume this would be quite simple. Thanks. Quote Link to comment Share on other sites More sharing options...
cags Posted November 16, 2009 Share Posted November 16, 2009 Assuming the string is always in that exact form you could use something along the lines of... '#token=([^"]+)#' I'd imagine the token would have a specific length as it is likely a hash? If thats the case then you could make a slightly more specific pattern. Hopefully that makes sense I have had a pint or six . Quote Link to comment Share on other sites More sharing options...
marklarah Posted November 16, 2009 Author Share Posted November 16, 2009 Aye...it's 32 characters long everytime, so it is a hash, you're quite correct. However, what you gave me works perfectly! I love you! Thanks...I didn't really think about ignoring the rest of the string... Quote Link to comment Share on other sites More sharing options...
cags Posted November 16, 2009 Share Posted November 16, 2009 Glad it's working, as it's a hash you could probably make it a bit more accurate using something like... '#token=([a-z1-9]{32})#' That way your not reliant on the token being the last parameter in the URL. 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.