I want to remove te first part of the url in PHP. 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). How to do this?
There is a built in php function for this parse_url.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With