Extract first URL segment from full URL

How can I extract the first url from a full url? The first segment of the URL must be cleared to replace with - space .

Full URL

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

Desired Performance

 River Island 
+6
source share
5 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 (/) 
+12
source
 $page = explode('/', substr($_SERVER['REQUEST_URI'], 1), 2); echo str_replace("-"," ", $page[0]); 
+2
source

Try the following: /http:\/\/[^\/]+\/([^\/]+)/i

See here: http://regex101.com/r/lB9jN7

+1
source
 $path = parse_url($url, PHP_URL_PATH); $first = substr($path, 0, strpos($path, '/')); 

Check the docs for these three features. You may have to remove the slash from the beginning of the path, I'm not sure.

0
source

Do you have CodeIgniter ... ???, then it could be

 $this->uri->segment(segment number of url); 

and it needs to load uri library in Controller

-2
source

Source: https://habr.com/ru/post/923423/


All Articles