I need to remove every characters before the last "/"
This my url :
http://www.example.com/highlights/cat/all-about-clothing/
And I want to have only :
all-about-clothing
Thanks
I need to remove every characters before the last "/"
This my url :
http://www.example.com/highlights/cat/all-about-clothing/
And I want to have only :
all-about-clothing
Thanks
Use basename()
$str = 'http://www.example.com/highlights/cat/all-about-clothing/';
echo basename($str);
// Outputs: all-about-clothing
EDIT:
Another Solution:
$str = 'http://www.example.com/highlights/cat/all-about-clothing/';
$path = pathinfo($str, PATHINFO_BASENAME);
echo "<br/>" . $path;
$str = 'http://www.example.com/highlights/cat/all-about-clothing/';
$str = trim($str,'/');
$str = explode('/',$str);
echo $str = end($str);
// get result
all-about-clothing
<?php
$url = "http://www.example.com/highlights/cat/all-about-clothing/";
$url_path = parse_url($url, PHP_URL_PATH);
$basename = pathinfo($url_path, PATHINFO_BASENAME);
echo $basename;
?>
<?php
$url = 'http://www.example.com/highlights/cat/all-about-clothing/';
$basename = split('/',$url);
echo $basename[5];
?>
Most of the above solution focus on the exact example URL, Please be careful as if extra params are added to the end of the string, you may get the wrong result:
http://www.example.com/highlights/cat/all-about-clothing/?page=1 http://www.example.com/highlights/cat/all-about-clothing/item/1
to cache the 3rd "directory" after the domain name, and ignore the rest of the URL, the following code can be used:
$url = "http://www.example.com/highlights/cat/all-about-clothing/item/1";
$url_path = parse_url($url, PHP_URL_PATH); # /highlights/cat/all-about-clothing/item/1
$dirs = explode('/', $url_path); # Array([0] =>"", [1]=>"highlights", [2]=>"cat", [3]=>"all-about-clothing", [4]=>"item", [5]=>"1")
echo $dirs[3]; # all-about-clothing