• 0

Search a string (php)


Question

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?

Link to comment
https://www.neowin.net/forum/topic/1059104-search-a-string-php/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

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];

  • 0

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);[/CODE]

  • 0

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);[/CODE]

Nice one!

  • 0

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);[/CODE]

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Wow, imagine you dump hundreds of hours into completing things and unlocking stuff and you lose it all. Back in the day when cheats were built into games, you could at least unlock things again that way without spending hundreds of hours again. But those days are long gone for some reason as no one builds cheats into games anymore. So it's even more painful that studio that's on its 6th installment **** it up so badly.
    • Spotify finally removes the disco ball app icon in the latest update by Ivan Jenic Image: Spotify Spotify has just released an update that removes its now infamous disco ball icon. The update reverts the app icon to the familiar flat green logo after weeks of mixed reactions online. The icon arrived on May 13 as part of the company's 20th anniversary celebration and was always intended to be temporary, though Spotify only confirmed that after the backlash started. The disco ball took the internet by storm, as the reception was split. A vocal group of users called it ugly and disorienting, with some iOS users noting that the 3D glowing effect made the app look like it was stuck mid-update. On the other end, the icon picked up a following of its own. Its retro, three-dimensional look immediately stood out against the flat, minimalist aesthetic that has dominated app design for years. It even started a small movement, spawning what people started calling "discomorphism," a mashup of disco and skeuomorphism. Other brands started posting disco ball versions of their own logos, probably in an effort to ride the wave of memes that flooded the internet during late May. Spotify has had a turbulent relationship with its user base lately. Besides the disco ball icon, which certainly wasn't appreciated by everyone, the company has also received backlash for its willingness to include AI-generated music on its platform. On May 17, Spotify promised the old icon would return “in a few weeks.” And now it looks like that time has finally arrived. So, whether you liked the disco ball or it made you uncomfortable, it’s now gone for good. The next time you update the Spotify app on your phone, the old, flat-design icon will return.
  • Recent Achievements

    • One Year In
      slackerzz earned a badge
      One Year In
    • One Year In
      highriskpaym earned a badge
      One Year In
    • One Month Later
      highriskpaym earned a badge
      One Month Later
    • Week One Done
      highriskpaym earned a badge
      Week One Done
    • Week One Done
      FBSPL earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      520
    2. 2
      PsYcHoKiLLa
      197
    3. 3
      +Edouard
      157
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!