Scheduled PSN downtime tomorrow in Back Page News

Search a string (php)


4 replies to this topic - - - - -

#1 Banzai

    Neowinian Senior

  • 3,387 posts
  • Joined: 20-September 03
  • Location: UK, Exeter

Posted 20 February 2012 - 10:52

Hello I have a string

$str = 'HTTP/1.1 401 Unauthorized Cache-Control: max-age=0 WWW-Authenticate: Digest realm="this web site", nonce="4f4219a0543637c4f", stale="false", algorithm="MD5", qop="auth" Date: Mon, 20 Feb 2012 10:13:23 GMT Server: lighttpd HTTP/1.1 302 Found Cache-Control: max-age=0 Cache-Control: must revalidate Location: u_lastlogin.htm?message=20-02-2012 10:09;0;Web Content-Type: text/html Set-Cookie: session_id=b71b8885254c6e48; discard; security Set-Cookie: loggedin=true;Cache-control: no-cache=set-cookie Date: Mon, 20 Feb 2012 10:13:29 GMT Server: lighttpd ';

and I want to extract session_id, I think this is done with preg_match but I have no idea how to write a regular expression that will extract 'b71b8885254c6e48' any ideas?


#2 Banzai

    Neowinian Senior

  • 3,387 posts
  • Joined: 20-September 03
  • Location: UK, Exeter

Posted 20 February 2012 - 11:06

Ok sorry for posting figured it out

$result = 'HTTP/1.1 401 Unauthorized Cache-Control: max-age=0 WWW-Authenticate: Digest realm="this web site", nonce="4f4219a0543637c4f", stale="false", algorithm="MD5", qop="auth" Date: Mon, 20 Feb 2012 10:13:23 GMT Server: lighttpd HTTP/1.1 302 Found Cache-Control: max-age=0 Cache-Control: must revalidate Location: u_lastlogin.htm?message=20-02-2012 10:09;0;Web Content-Type: text/html Set-Cookie: session_id=b71b8885254c6e48; discard; security Set-Cookie: loggedin=true;Cache-control: no-cache=set-cookie Date: Mon, 20 Feb 2012 10:13:29 GMT Server: lighttpd ';

preg_match('/session_id=(.*?);/', $result, $matches);

echo $matches[1];

#3 +Phouchg

    Internets Loser

  • 2,355 posts
  • Joined: 28-March 11
  • Location: Past Imperfect
  • OS: WretchedOS 6.1.7601 x64

Posted 20 February 2012 - 13:20

I'm being picky but I might add that regular expressions in this case might be suboptimal. There's three things why so: you know the exact string you're looking for, its length is constant and there's no more than a single occurence of it.

echo substr($result, strpos($result, '_id=') + 4, 16);


#4 Tekkerson

    Droppin' them beats like hot potatoes.

  • 1,274 posts
  • Joined: 05-March 08
  • Location: Dallas, TX

Posted 20 February 2012 - 13:24

View Postcralias, on 20 February 2012 - 13:20, said:

I'm being picky but I might add that regular expressions in this case might be suboptimal. There's three things why so: you know the exact string you're looking for, its length is constant and there's no more than a single occurence of it.

echo substr($result, strpos($result, '_id=') + 4, 16);

Nice one!

#5 Banzai

    Neowinian Senior

  • 3,387 posts
  • Joined: 20-September 03
  • Location: UK, Exeter

Posted 20 February 2012 - 18:32

View Postcralias, on 20 February 2012 - 13:20, said:

I'm being picky but I might add that regular expressions in this case might be suboptimal. There's three things why so: you know the exact string you're looking for, its length is constant and there's no more than a single occurence of it.

echo substr($result, strpos($result, '_id=') + 4, 16);

Ah thanks cralias, I thought of doing it like that but couldn't think how to write it.