Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove first part of url in PHP?

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?

like image 561
user735795 Avatar asked May 03 '11 09:05

user735795


2 Answers

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
like image 191
amccormack Avatar answered Oct 13 '22 08:10

amccormack


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

like image 44
Yoshi Avatar answered Oct 13 '22 09:10

Yoshi