[英]How to remove first part of url in PHP?
I want to remove te first part of the url in PHP.我想删除 PHP 中 url 的第一部分。 Example:例子:
http://www.domain.com/sales
http://otherdomain.org/myfolder/seconddir
/directory
must be:一定是:
/sales
/myfolder/seconddir
/directory
Because the url in dynamic, I think I have to do this with preg replace, but I don't know how.. And sometimes the url is already removed (see last example).因为 url 是动态的,我想我必须用 preg 替换来做到这一点,但我不知道怎么做。有时 url 已经被删除(见最后一个例子)。 How to do this?这个怎么做?
There is a built in php function for this parse_url .这个parse_url有一个内置的 php function 。
From the linked website:从链接的网站:
<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
The above example will output:上面的例子将 output:
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
/path
Try:尝试:
<?php
$url = 'http://otherdomain.org/myfolder/seconddir';
$urlParts = parse_url($url);
print_r($urlParts);
And have a look at:看看:
http://php.net/manual/en/function.parse-url.php http://php.net/manual/en/function.parse-url.php
you could use path info :你可以使用路径信息:
<?php
print_r(pathinfo("http://www.domain.com/sales"));
print_r(pathinfo("http://otherdomain.org/myfolder/seconddir"));
print_r(pathinfo("/directory"));
?>
the output: output:
Array
(
[dirname] => http://www.domain.com
[basename] => sales
[filename] => sales
)
Array
(
[dirname] => http://otherdomain.org/myfolder
[basename] => seconddir
[filename] => seconddir
)
Array
(
[dirname] => /
[basename] => directory
[filename] => directory
)
good luck!祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.