5

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

Kaherdin
  • 2,055
  • 3
  • 23
  • 34
  • You can catch this using regex: `http:\/\/www\.example\.com\/highlights\/cat\/(.*) ` https://regex101.com/r/tG5pA6/1 – bodruk Nov 03 '15 at 12:56

7 Answers7

7

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;
Pupil
  • 23,834
  • 6
  • 44
  • 66
  • 1
    Also check [pathinfo()](http://php.net/manual/de/function.pathinfo.php) with PATHINFO_BASENAME as `basename()` was meant for directory paths. – pmayer Nov 03 '15 at 12:49
  • Ok, forget about that. Esentially `pathinfo()` and `basename()` do the same. – pmayer Nov 03 '15 at 12:52
  • 1
    @PatrikMayer, you are right. I have updated my answer and added your suggestions. – Pupil Nov 03 '15 at 12:53
  • Thank you. Maybe keep the second parameter of `pathinfo()` in mind. Set to `PATHINFO_BASENAME` it returns the basename directly. – pmayer Nov 03 '15 at 12:55
2

Use PHP's parse_url() function.

edit: basename() or pathinfo() is the easier way.

pmayer
  • 341
  • 2
  • 13
2
$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

ImBhavin95
  • 1,494
  • 2
  • 16
  • 29
0
<?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;
?>
Siddhartha esunuri
  • 1,104
  • 1
  • 17
  • 29
  • That's not right. [pathinfo()](http://php.net/manual/de/function.pathinfo.php) wants the URL as first parameter, not the array given by parse_url. – pmayer Nov 03 '15 at 12:48
0

You can use regex too:

$match = [];
$subject = 'http://www.example.com/highlights/cat/all-about-clothing/';
$pattern = '/http:\/\/www\.example\.com\/highlights\/cat\/(.*)/';
preg_match($pattern, $subject, $match);
print_r($match);

You can see the result here.

bodruk
  • 3,242
  • 8
  • 34
  • 52
0
<?php
$url = 'http://www.example.com/highlights/cat/all-about-clothing/';
$basename =  split('/',$url);    
echo $basename[5];
?>
0

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
d.raev
  • 9,216
  • 8
  • 58
  • 79