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