我有一个网站,我需要得到一个网页的URL与PHP。 网址可能是东西www.mydomain.com/thestringineed/
,也可以www.mydomain.com/thestringineed?data=1
或可以www.mydomain.com/ss/thestringineed
所以它总是最后一个字符串,但我不想以后得到什么?
我有一个网站,我需要得到一个网页的URL与PHP。 网址可能是东西www.mydomain.com/thestringineed/
,也可以www.mydomain.com/thestringineed?data=1
或可以www.mydomain.com/ss/thestringineed
所以它总是最后一个字符串,但我不想以后得到什么?
您将使用parse_url功能,再看看返回的路径部分。 像这样:
$url='www.mydomain.com/thestringineed?data=1';
$components=parse_url($url);
//$mystring= end(explode('/',$components['path']));
// I realized after this answer had sat here for about 3 years that there was
//a mistake in the above line
// It would only give the last directory, so if there were extra directories in the path, it would fail. Here's the solution:
$mystring=str_replace( reset(explode('/',$components['path'])),'',$components['path']); //This is to remove the domain from the beginning of the path.
// In my testing, I found that if the scheme (http://, https://, ...) is present, the path does not include
//the domain. (it's available on it's own as ['host']) In that case it's just
// $mystring=$components['path']);
parse_url
应该帮助你。
<?php
$url = "http://www.mydomain.com/thestringineed/";
$parts = parse_url($url);
print_r($parts);
?>
parse_url()
是您正在寻找的功能。 你想确切的部分,可以通过接收PHP_URL_PATH
$url = 'http://php.net/manual/en/function.parse-url.php';
echo parse_url($url, PHP_URL_PATH);
使用$_SERVER['REQUEST_URI']
会返回完整当前页面的URL,你可以用“/”分开,并利用最后的数组索引。 这将是最后一个字符串
您可以使用:
$strings = explode("/", $urlstring);
这将删除所有的“/”在url并返回包含的所有单词的数组。
$strings[count($strings)-1]
现在有你需要的字符串值,但它可能包含,所以我们需要删除“数据= 1?”:
$strings2 = explode("?", $strings[count($strings)-1]);
$strings2[0]
有你是想出来的URL字符串。
希望这可以帮助!
<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
和你出来卖出期权
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
/path