Reputation: 327
$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
Upvotes: 15
Views: 25702
Reputation: 57656
For Laravel
dirname(url()->current())
In url()->current() -> you will get current URL.
In dirname -> You will get parent directory.
In Core PHP:
dirname($currentURL)
Upvotes: 4
Reputation: 14490
For the one-liners:
$url = implode('/', array_splice( explode('/', $articleimage), 0, -1 ) );
Upvotes: 0
Reputation: 3695
Here's the simple way to achieve
str_replace(basename($articleimage), '', $articleimage);
Upvotes: 0
Reputation: 7822
There is no need to use explode
, implode
, and array_pop
.
Just use dirname($path)
. It's a lot more efficient and cleaner code.
Upvotes: 11
Reputation: 4249
It looks like this may be what you are looking for. Instead of exploding and imploding, you can use the parsing functions which are designed to handle exactly this kind of URL manipulation.
$url = parse_url( $url_string );
$result =
$url['scheme']
. "://"
. $url['host']
. pathinfo($url['path'], PATHINFO_DIRNAME );
Upvotes: 0
Reputation: 1770
Use the following string manipulation from PHP
$url_without_last_part = substr($articleimage, 0, strrpos($articleimage, "/"));
Upvotes: 4
Reputation: 20286
Pretty simple solution add in the end of your code
$url = implode('/', $url);
echo $url;
Notice that array_pop use reference argument passing so array will be modifed implode() function does the opposite to explode function and connects array elements by first argument(glue) and returns the string.
Upvotes: 0
Reputation: 2182
after the array_pop
you can do
$url2=implode("/",$url)
to get the url in a string
Upvotes: 1
Reputation: 5512
Try this:
$url = explode('/', 'http://www.brightknowledge.org/knowledge-bank/media/studying-media/student-media/image_rhcol_thin');
array_pop($url);
echo implode('/', $url);
Upvotes: 26
Reputation: 46
Change this:
$articleurl = array_pop($url);
Into this:
$articleurl = end($url);
$articleurl will then hold the last array key.
Missed the part where you want to remove the value, you can use the function key() to get the key and then remove the value using that key
$array_key = key($articleurl);
unset(url[$array_key])
Upvotes: 0