7

如何从完整的 URL 中提取第一个 URL 段?应清理第一个 URL 段以-用空格替换

完整网址

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

期望的输出

River Island
4

5 回答 5

14

您可以使用:

$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 (/)
于 2012-08-21T19:18:16.947 回答
2
$page = explode('/', substr($_SERVER['REQUEST_URI'], 1), 2);
echo str_replace("-"," ", $page[0]);
于 2012-08-21T19:19:11.050 回答
1

试试这个:/http:\/\/[^\/]+\/([^\/]+)/i

见这里:http ://regex101.com/r/lB9jN7

于 2012-08-21T19:18:22.117 回答
0
$path = parse_url($url, PHP_URL_PATH);
$first = substr($path, 0, strpos($path, '/'));

检查这三个功能的文档。也许你必须从路径的开头去掉一个斜线,我不确定。

于 2012-08-21T19:19:18.793 回答
-2

你有没有使用 CodeIgniter ...???那么它可能是

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

以及它需要在Controller中加载uri库

于 2012-08-22T11:52:04.190 回答