Extract first URL Segment from full URL

被刻印的时光 ゝ 提交于 2019-12-09 17:38:32

问题


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

回答1:


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 (/)



回答2:


$page = explode('/', substr($_SERVER['REQUEST_URI'], 1), 2);
echo str_replace("-"," ", $page[0]);



回答3:


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

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




回答4:


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

Check the docs for these three functions. Maybe you'll have to strip a slash from the beginning of the path, I'm not sure.




回答5:


have you using CodeIgniter ...???then it could be

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

and its need to load uri library in Controller



来源:https://stackoverflow.com/questions/12061528/extract-first-url-segment-from-full-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!