如何从完整网址中提取第一个网址段?应清理第一个网址细分,将-
替换为空格。
完整网址
http://www.domain.com/River-Island/River-Island-T-Shirt-with-Triangle-Girl-Print/Prod/pgeproduct.aspx?iid=2516020
渴望的超越
River Island
答案 0 :(得分: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 (/)
答案 1 :(得分:2)
$page = explode('/', substr($_SERVER['REQUEST_URI'], 1), 2);
echo str_replace("-"," ", $page[0]);
答案 2 :(得分:1)
试试这个:/http:\/\/[^\/]+\/([^\/]+)/i
答案 3 :(得分:0)
$path = parse_url($url, PHP_URL_PATH);
$first = substr($path, 0, strpos($path, '/'));
检查这三个功能的文档。也许你不得不从路径的开头剥去斜线,我不确定。
答案 4 :(得分:-2)
你使用CodeIgniter ...... ???那么它可能是
$this->uri->segment(segment number of url);
需要在Controller中加载uri库