URL。仅重写脚本名称和查询字符串

时间:2015-06-18 15:43:03

标签: php url

完整网址:http://www.somepage.com/admin/index.php?do=manageNews&search=1&sort=title&order=ASC

如何仅检索此部分:index.php?do=manageNews&search=1&sort=title&order=ASC

1 个答案:

答案 0 :(得分:0)

您可以使用parse_url()pathinfo()功能的组合,如下所示:

$url = 'http://www.somepage.com/admin/index.php?do=manageNews&search=1&sort=title&order=ASC';

# retrieve index.php file name using pathinfo() and parse_url()
$file = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_BASENAME);

# retrive query string using parse_url()
$query = parse_url($url, PHP_URL_QUERY);

# combine file name and query string.
if (strlen($query)>0) {
    $file .= '?' . $query;
}

echo "$file\n"; # index.php?do=manageNews&search=1&sort=title&order=ASC