Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract first URL Segment from full URL

Tags:

regex

php

slug

How can the first URL segment be extracted from the full URL? The first URL segment should be cleaned to replace the - with a space .

Full URL

http://www.domain.com/River-Island/River-Island-T-Shirt-with-Triangle-Girl-Print/Prod/pgeproduct.aspx?iid=2516020

Desired Outpput

River Island
like image 777
Nyxynyx Avatar asked Aug 21 '12 19:08

Nyxynyx


2 Answers

You can use:

$url = 'http://www.domain.com/River-Island/River-Island-T-Shirt-with-Triangle-Girl-Print/Prod/pgeproduct.aspx?iid=2516020';
$parsed = parse_url($url);
$path = $parsed['path'];
$path_parts = explode('/', $path);
$desired_output = $path_parts[1]; // 1, because the string begins with slash (/)
like image 132
Teneff Avatar answered Oct 06 '22 13:10

Teneff


$page = explode('/', substr($_SERVER['REQUEST_URI'], 1), 2);
echo str_replace("-"," ", $page[0]);
like image 29
Green Black Avatar answered Oct 06 '22 13:10

Green Black