Por
Por

Reputation: 423

Remove some slug from url

I have difficulties in remove some slug from the url.

I have some urls like these below

http://domain1.com/so-section/upload/image1.jpg
http://domain2example.com/so-section/upload/image2.jpg
http://domain3place.com/so-section/upload/image3.jpg
http://domain4code.com/so-section/upload/image4.jpg
http://domain5action.com/so-section/upload/image5.jpg
http://domain6rack.com/so-section/upload/image5.jpg

Obviously, you will see domain are unsame, So I only would like to get "/so-section/upload/imagename.jpg" from the url. Would that be possible to remove whatever domain name come! Please help!

Upvotes: 0

Views: 131

Answers (3)

Charles Butler
Charles Butler

Reputation: 573

A simple solution to get everything after the domain name.

function GetJunk($url)
{
   $start = explode("/",$url); //get everything between "/"
   array_shift($start);array_shift($start);array_shift($start); //shift sections
   $junk = implode("/",$start); //get everything after the new shifted sections
   return $junk; //  so-section/upload/image2.jpg
};

Then to use you simply:

echo GetJunk("http://domain2example.com/so-section/upload/image2.jpg");

Here is a working example: http://phpfiddle.org/main/code/4sr-zrm

Hope this helps!

Upvotes: 0

makallio85
makallio85

Reputation: 1356

 $pieces = explode('/', $url);
 echo '/'. $pieces[4].'/'.$pieces[5].'/'.$pieces[6];

Upvotes: 0

dave
dave

Reputation: 64657

You can use parse_url and then get the path:

$url = parse_url("http://domain1.com/so-section/upload/image1.jpg");
echo $url["path"]; ///so-section/upload/image1.jpg

Upvotes: 2

Related Questions