Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove last part of url in PHP

Tags:

php

 $url = explode('/', $articleimage);
 $articleurl = array_pop($url);

I have used the above method to get the last part of a URL.Its working.But I want to remove the last part from the URL and display the remaining part.Please help me.Here I am mentioning the example URL.

http://www.brightknowledge.org/knowledge-bank/media/studying-media/student-media/image_rhcol_thin     
like image 729
Nithinkumar CN Avatar asked Sep 15 '14 14:09

Nithinkumar CN


3 Answers

Try this:

$url = explode('/', 'http://www.brightknowledge.org/knowledge-bank/media/studying-media/student-media/image_rhcol_thin');
array_pop($url);
echo implode('/', $url); 
like image 86
Bogdan Burym Avatar answered Nov 07 '22 10:11

Bogdan Burym


There is no need to use explode, implode, and array_pop.

Just use dirname($path). It's a lot more efficient and cleaner code.

like image 37
Dan Bray Avatar answered Nov 07 '22 11:11

Dan Bray


Use the following string manipulation from PHP

$url_without_last_part = substr($articleimage, 0, strrpos($articleimage, "/"));
like image 3
DJ' Avatar answered Nov 07 '22 11:11

DJ'