从URL中提取查询字符串的一部分 [英] Extract part from URL for a query string

查看:266
本文介绍了从URL中提取查询字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提取URL的特定部分.

I need a certain part of a URL extracted.

示例:

http://www.domain.com/blog/entry-title/?standalone=1是给定的URL.

blog/entry-title应该被提取.

但是,提取也应该使用http://www.domain.com/index.php/blog/[…]作为给定的URL.

However, the extraction should also work with http://www.domain.com/index.php/blog/[…]as the given URL.

此代码用于内容管理系统.

This code is for a Content Management System.

我已经想到的是这样:

function getPathUrl() {

    $folder = explode('/', $_SERVER['SCRIPT_NAME']);
    $script_filename = pathinfo($_SERVER['SCRIPT_NAME']); // supposed to be 'index.php'
    $request = explode('/', $_SERVER['REQUEST_URI']);

    // first element is always ""
    array_shift($folder);
    array_shift($request);

    // now it's only the request url. filtered out containing folders and 'index.php'.
    $final_request = array_diff($request, array_intersect($folder, $request));

    // the indexes are mangled up in a strange way. turn 'em back
    $final_request = array_values($final_request);

    // remove empty elements in array (caused by superfluent slashes, for instance)
    array_clean($final_request);

    // make a string out of the array
    $final_request = implode('/', $final_request);

    if ($_SERVER['QUERY_STRING'] || substr($final_request, -1) == '?') {
        $final_request = substr($final_request, 0, - strlen($_SERVER['QUERY_STRING']) - 1);
    }

    return $final_request;

}

但是,此代码不处理URL末尾的参数(如?standalone=1).不过,它适用于锚点(#read-more).

However, this code does not take care of the arguments at the end of the URL (like ?standalone=1). It works for anchors (#read-more), though.

感谢很多人,并且开心地绞尽脑汁.也许我们可以用正则表达式来做到这一点.

Thanks a ton guys and have fun twisting your brains. Maybe we can do this shit with a regular expression.

推荐答案

有许多示例和您想要的信息:

There's many examples and info for what you want at:

http://php.net/manual/en/function.parse -url.php

这篇关于从URL中提取查询字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆