Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

parsing - PHP - parse current URL

I need to parse the current url so that, in either of these cases:

http://mydomain.com/abc/
http://www.mydomain.com/abc/

I can get the return value of "abc" (or whatever text is in that position). How can I do that?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use parse_url();

$url = 'http://www.mydomain.com/abc/';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);

which would give you

Array
(
    [scheme] => http
    [host] => www.mydomain.com
    [path] => /abc/
)
/abc/

Update: to get current page url and then parse it:

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

print_r(parse_url(curPageURL()));

echo parse_url($url, PHP_URL_PATH);

source for curPageURL function


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...