Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all characters before last / in URL [duplicate]

Tags:

php

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

like image 203
Kaherdin Avatar asked Nov 03 '15 12:11

Kaherdin


2 Answers

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;
like image 181
Pupil Avatar answered Sep 30 '22 20:09

Pupil


Use PHP's parse_url() function.

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

like image 39
pmayer Avatar answered Sep 30 '22 20:09

pmayer