Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get slug from current url

Tags:

php

I have URLs like so:

http://localhost/hi-every-body/
http://s1.localhost/hello-world/
http://s2.localhost/bye-world/

I want the page "slug" from the URLS, eg.

hi-every-body
hello-world
bye-world

What's a simple way of doing this in PHP?

like image 906
PHP Ferrari Avatar asked Apr 05 '13 09:04

PHP Ferrari


Video Answer


1 Answers

This should do exactly that:

trim(parse_url($url, PHP_URL_PATH), '/');

It takes the path and strips the forward slashes on both sides.

To get only the last part of the path:

basename(parse_url($url, PHP_URL_PATH));
like image 178
Ja͢ck Avatar answered Sep 27 '22 18:09

Ja͢ck