Re: URL parsing
| From: | 1LT John W. Holmes | Date: | Wed, 18 Dec 2002 15:41:36 +0000 |
| Subject: | Re: URL parsing | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-128731@lists.php.net to get a copy of this message | ||
> http://www.php.net/manual/en/function.parse-url.php
If you're going to help, at least read the question. The poster already said
they tried that and it's not even a solution, anyhow. The "username" it
refers to in parse_url() is for URLs in the format of
http://username:password@www.domain.com.
> >http://www.naturalist.com/~fungae/index.php
> >
> >which is stored in $http_referer (as parse_url from
> >$HTTP_REFERER).
> >
> >I'm trying to extract the username (~fungae). I've
> >read the docs on parse_url(), and have tried to get
> >$http_referer[user], but it comes up with zilch. I've
> >also tried to print_r $http_referer, but I only get
> >scheme, host, path, and query. Any ideas?
Assuming $url is what you have above:
$tilde = strpos($url,"~");
$slash = strpos($url,"/",$tilde);
$length = $slash - $tilde;
$username = substr($url,$tilde,$length);
Or you can use a regular expression, but the above is probably faster.
preg_match("!(~[^/]*)/!",$url,$match);
or
preg_match("!(~.*)/!U",$url,$match);
In my tests, the first solution (using strpos) was the fastest by 35%.
---John Holmes...