提取完整的URL第一个URL段(Extract first URL Segment from ful

2019-07-31 02:33发布

如何能在第一URL段从完整的URL中提取? 第一个URL段应清洗更换-用空格

全网址

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

期望Outpput

River Island

Answer 1:

您可以使用:

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


Answer 2:

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


Answer 3:

尝试这样的: /http:\/\/[^\/]+\/([^\/]+)/i

在这里看到: http://regex101.com/r/lB9jN7



Answer 4:

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

检查这三个函数的文档。 也许你将不得不从路径的开始剥离斜线,我不知道。



Answer 5:

你用笨... ???那么它可能是

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

其需要加载控制器中的URI库



文章来源: Extract first URL Segment from full URL
标签: php regex slug