Re: stripping text from a string
| From: | sylikc | Date: | Fri, 29 Oct 2004 21:40:12 +0000 |
| Subject: | Re: stripping text from a string | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-200689@lists.php.net to get a copy of this message | ||
Adam,
> Hi, I use a piece of proprietary software at work that uses weird session
> ID strings in the URL. A sample URL looks like:
>
>
> http://zed2.mdah.state.ms.us/F/CC8V7H1JF4LNBVP5KARL4KGE8AHIKP1I72JSBG6AYQSMK8YF4Y-01471?func=find-b-0
>
> The weird session ID string changes each time you login. Anyway, how can
> I strip out all of the text between the last / and the ? and insert it
> into another variable? So in this case, the end result would be a
> variable containing:
>
> CC8V7H1JF4LNBVP5KARL4KGE8AHIKP1I72JSBG6AYQSMK8YF4Y-01471
You can use the parse_url() function to get just the path, and then
use strrpos() to find the last "/" character. Finally, use substr()
to extract the piece that you wanted.
Example:
$url_array = parse_url('http://zed2.mdah.state.ms.us/F/CC8V7H1JF4LNBVP5KARL4KGE8AHIKP1I72JSBG6AYQSMK8YF4Y-01471?func=find-b-0');
$slash_pos = strrpos($url_array['path'],'/');
$session_ID = substr($url_array['path'],$slash_pos+1);
/sylikc