Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: parse_url - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

rawurldecode" width="11" height="7"/> <http_build_query
Last updated: Thu, 31 May 2007

view this page in

parse_url

(PHP 4, PHP 5)

parse_url — URL を解釈し、その構成要素を返す

説明

mixed parse_url ( string $url [, int $component] )

この関数は、URL の様々な構成要素のうち特定できるものに関して 連想配列にして返します。

この関数は、指定された URL が有効かどうかを調べるためのもの ではなく、単に URL を上で示した 要素に分解するだけのものです。不完全な URL であっても受け入れられますし、 そのような場合でも parse_url() は可能な限り 正しく解析しようとします。

パラメータ

url

パースする URL

component

PHP_URL_SCHEMEPHP_URL_HOSTPHP_URL_PORTPHP_URL_USERPHP_URL_PASSPHP_URL_PATHPHP_URL_QUERY あるいは PHP_URL_FRAGMENT のうちのいずれかを指定し、 特定の URL コンポーネントのみを 文字列で取得するようにします。

返り値

完全におかしな形式の URL については、parse_url()FALSE を返し、E_WARNING を発生します。それ以外の場合は 連想配列が返され、その中には以下の要素(のうち少なくともひとつ)が含まれます。

  • scheme - 例: http
  • host
  • port
  • user
  • pass
  • path
  • query - クエスチョンマーク ? 以降
  • fragment - ハッシュマーク # 以降

component が指定されている場合、結果は array ではなく文字列で返されます。

変更履歴

バージョン説明
5.1.2パラメータ component が追加されました。

例 2502. parse_url() の例

<?php
$url
= 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));
?>

上の例の出力は以下となります。


Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)

    

注意

注意: この関数は相対 URL では動作しません。

注意: parse_url() は URL をパースするための関数であり、 URI をパースするものではありません。しかし、PHP の後方互換性を満たすため、 例外として file:// スキームについては 3 重スラッシュ(file:///...) が認められています。他のスキームにおいては、これは無効な形式となります。

参考

pathinfo()
parse_str()
dirname()
basename()



rawurldecode" width="11" height="7"/> <http_build_query
Last updated: Thu, 31 May 2007
 
add a note add a note User Contributed Notes
parse_url
Michael Muryn
28-Aug-2007 12:51
Another update to the glue_url function: applied the "isset" treatment to $parsed['pass'].

<?php
function glue_url($parsed)
{
    if (!
is_array($parsed)) return false;
   
$uri = isset($parsed['scheme']) ? $parsed['scheme'].':'.((strtolower($parsed['scheme']) == 'mailto') ? '' : '//') : '';
   
$uri .= isset($parsed['user']) ? $parsed['user'].(isset($parsed['pass']) ? ':'.$parsed['pass'] : '').'@' : '';
   
$uri .= isset($parsed['host']) ? $parsed['host'] : '';
   
$uri .= isset($parsed['port']) ? ':'.$parsed['port'] : '';
    if(isset(
$parsed['path']))
    {
       
$uri .= (substr($parsed['path'], 0, 1) == '/') ? $parsed['path'] : ('/'.$parsed['path']);
    }
   
$uri .= isset($parsed['query']) ? '?'.$parsed['query'] : '';
   
$uri .= isset($parsed['fragment']) ? '#'.$parsed['fragment'] : '';
    return
$uri;
}
?>
stevenlewis at hotmail dot com
13-Aug-2007 07:08
an update to the glue url function.

you are able to put a host and a path without a slash at the beginning of the path

<?php
function glue_url($parsed)
    {
    if (!
is_array($parsed)) return false;
   
$uri = isset($parsed['scheme']) ? $parsed['scheme'].':'.((strtolower($parsed['scheme']) == 'mailto') ? '':'//'): '';
   
$uri .= isset($parsed['user']) ? $parsed['user'].($parsed['pass']? ':'.$parsed['pass']:'').'@':'';
   
$uri .= isset($parsed['host']) ? $parsed['host'] : '';
   
$uri .= isset($parsed['port']) ? ':'.$parsed['port'] : '';
    if(isset(
$parsed['path']))
        {
       
$uri .= (substr($parsed['path'],0,1) == '/')?$parsed['path']:'/'.$parsed['path'];
        }
   
$uri .= isset($parsed['query']) ? '?'.$parsed['query'] : '';
   
$uri .= isset($parsed['fragment']) ? '#'.$parsed['fragment'] : '';
    return
$uri;
    }
?>
spam at paulisageek dot com
09-Aug-2007 04:05
In reply to adrian,

Thank you very much for your function. There is a small issue with your relative protocol function. You need to remove the // when making the url the path. Here is the new function.

function resolve_url($base, $url) {
        if (!strlen($base)) return $url;
        // Step 2
        if (!strlen($url)) return $base;
        // Step 3
        if (preg_match('!^[a-z]+:!i', $url)) return $url;
        $base = parse_url($base);
        if ($url{0} == "#") {
                // Step 2 (fragment)
                $base['fragment'] = substr($url, 1);
                return unparse_url($base);
        }
        unset($base['fragment']);
        unset($base['query']);
        if (substr($url, 0, 2) == "//") {
                // Step 4
                return unparse_url(array(
                        'scheme'=>$base['scheme'],
                        'path'=>substr($url,2),
                ));
        } else if ($url{0} == "/") {
                // Step 5
                $base['path'] = $url;
        } else {
                // Step 6
                $path = explode('/', $base['path']);
                $url_path = explode('/', $url);
                // Step 6a: drop file from base
                array_pop($path);
                // Step 6b, 6c, 6e: append url while removing "." and ".." from
                // the directory portion
                $end = array_pop($url_path);
                foreach ($url_path as $segment) {
                        if ($segment == '.') {
                                // skip
                        } else if ($segment == '..' && $path && $path[sizeof($path)-1] != '..') {
                                array_pop($path);
                        } else {
                                $path[] = $segment;
                        }
                }
                // Step 6d, 6f: remove "." and ".." from file portion
                if ($end == '.') {
                        $path[] = '';
                } else if ($end == '..' && $path && $path[sizeof($path)-1] != '..') {
                        $path[sizeof($path)-1] = '';
                } else {
                        $path[] = $end;
                }
                // Step 6h
                $base['path'] = join('/', $path);

        }
        // Step 7
        return unparse_url($base);
}
christian at resource-it dot dk
04-Aug-2007 04:57
I searched for an implementation of rfc3986, which is a newer version of rfc 2392. I may find it here : <" target="_blank">http://www.chrsen.dk/fundanemt/files/scripter/php/misc/rfc3986.php> - read the rfc at <" target="_blank">http://rfc.net/rfc3986.html>
adrian-php at sixfingeredman dot net
26-Jul-2007 06:58
Here's a function which implements resolving a relative URL according to RFC 2396 section 5.2. No doubt there are more efficient implementations, but this one tries to remain close to the standard for clarity. It relies on a function called "unparse_url" to implement section 7, left as an exercise for the reader (or you can substitute the "glue_url" function posted earlier).

<?php
/**
 * Resolve a URL relative to a base path. This happens to work with POSIX
 * filenames as well. This is based on RFC 2396 section 5.2.
 */
function resolve_url($base, $url) {
        if (!
strlen($base)) return $url;
       
// Step 2
       
if (!strlen($url)) return $base;
       
// Step 3
       
if (preg_match('!^[a-z]+:!i', $url)) return $url;
       
$base = parse_url($base);
        if (
$url{0} == "#") {
               
// Step 2 (fragment)
               
$base['fragment'] = substr($url, 1);
                return
unparse_url($base);
        }
        unset(
$base['fragment']);
        unset(
$base['query']);
        if (
substr($url, 0, 2) == "//") {
               
// Step 4
               
return unparse_url(array(
                       
'scheme'=>$base['scheme'],
                       
'path'=>$url,
                ));
        } else if (
$url{0} == "/") {
               
// Step 5
               
$base['path'] = $url;
        } else {
               
// Step 6
               
$path = explode('/', $base['path']);
               
$url_path = explode('/', $url);
               
// Step 6a: drop file from base
               
array_pop($path);
               
// Step 6b, 6c, 6e: append url while removing "." and ".." from
                // the directory portion
               
$end = array_pop($url_path);
                foreach (
$url_path as $segment) {
                        if (
$segment == '.') {
                               
// skip
                       
} else if ($segment == '..' && $path && $path[sizeof($path)-1] != '..') {
                               
array_pop($path);
                        } else {
                               
$path[] = $segment;
                        }
                }
               
// Step 6d, 6f: remove "." and ".." from file portion
               
if ($end == '.') {
                       
$path[] = '';
                } else if (
$end == '..' && $path && $path[sizeof($path)-1] != '..') {
                       
$path[sizeof($path)-1] = '';
                } else {
                       
$path[] = $end;
                }
               
// Step 6h
               
$base['path'] = join('/', $path);

        }
       
// Step 7
       
return unparse_url($base);
}
?>
Antti Haapala
17-Jul-2007 05:42
Actually the behaviour noticed by the previous poster is quite correct. When the URI scheme is not present, it is plain wrong to assume that something starting with www. is a domain name, and that the scheme is HTTP. Internet Explorer does it that way, sure, but it does not make it any more correct. The documentation says that the function tries to decode the URL as well as it can, and the only sensible and standards-compliant way to decode such URL is to expect it to be a relative URI.
Elliott Brueggeman
04-Jun-2007 07:59
Note that if you pass this function a url without a scheme (www.php.net, as opposed to http://www.php.net), the function will incorrectly parse the results. In my test case it returned the domain under the ['path'] element and nothing in the ['host'] element.
Marc-Antoine Ross
15-Mar-2007 01:10
Do not look for the fragment in $_SERVER['QUERY_STRING'], you will not find it. You should read the fragment in JavaScript for example.
alistair at 21degrees dot com dot au
24-Oct-2006 11:21
Heres a simple function to add the $component option in for PHP4. Haven't done exhaustive testing, but should work ok.

<?php

   
## Defines only available in PHP 5, created for PHP4
   
if(!defined('PHP_URL_SCHEME')) define('PHP_URL_SCHEME', 1);
    if(!
defined('PHP_URL_HOST')) define('PHP_URL_HOST', 2);
    if(!
defined('PHP_URL_PORT')) define('PHP_URL_PORT', 3);
    if(!
defined('PHP_URL_USER')) define('PHP_URL_USER', 4);
    if(!
defined('PHP_URL_PASS')) define('PHP_URL_PASS', 5);
    if(!
defined('PHP_URL_PATH')) define('PHP_URL_PATH', 6);
    if(!
defined('PHP_URL_QUERY')) define('PHP_URL_QUERY', 7);                       
    if(!
defined('PHP_URL_FRAGMENT')) define('PHP_URL_FRAGMENT', 8);   
   
    function
parse_url_compat($url, $component=NULL){
       
        if(!
$component) return parse_url($url);
       
       
## PHP 5
       
if(phpversion() >= 5)
            return
parse_url($url, $component);

       
## PHP 4
       
$bits = parse_url($url);
       
        switch(
$component){
            case
PHP_URL_SCHEME: return $bits['scheme'];
            case
PHP_URL_HOST: return $bits['host'];
            case
PHP_URL_PORT: return $bits['port'];
            case
PHP_URL_USER: return $bits['user'];
            case
PHP_URL_PASS: return $bits['pass'];
            case
PHP_URL_PATH: return $bits['path'];
            case
PHP_URL_QUERY: return $bits['query'];
            case
PHP_URL_FRAGMENT: return $bits['fragment'];
        }
       
    }

?>
dawalama at gmail dot com
05-Oct-2006 01:48
With few modifications

    /**
     * source: http://us2.php.net/manual/en/function.parse-url.php#60237
     * Edit the Query portion of an url
     *
     * @param    string    $action    ethier a "+" or a "-" depending on what action you want to perform
     * @param    mixed    $var    array (+) or string (-)
     * @param    string    $uri    the URL to use. if this is left out, it uses $_SERVER['PHP_SELF']
     * @version      1.0.0
     */
    function change_query($action, $var = NULL, $uri = NULL) {

               if (($action == "+" && !is_array($var)) || ($action == "-" && $var == "") || $var == NULL) {
                       return FALSE;
               }

               if (is_null($uri)) { //Piece together uri string
                       $beginning = $_SERVER['PHP_SELF'];
                       $ending = (isset ($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : '';
               } else {
                       $qstart = strpos($uri, '?');
                       if ($qstart === false) {
                               $beginning = $uri; //$ending is '' anyway
                               $ending = "";
                       } else {
                               $beginning = substr($uri, 0, $qstart);
                               $ending = substr($uri, $qstart);
                       }
               }

               $vals = array ();
               $ending = str_replace('?', '', $ending);
               parse_str($ending, $vals);

               switch ($action) {
                       case '+' :
                               $vals[$var[0]] = $var[1];
                               break;
                       case '-' :
                               if (isset ($vals[$var])) {
                                       unset ($vals[$var]);
                               }
                               break;
                       default :
                               break;
               }

               $params = array();
               foreach ($vals as $k => $value) {
                       $params[] = $k."=".urlencode($value);
               }
               $result = $beginning . (count($params) ? '?' . implode("&", $params) : '');
               return $result;
       }
kjensen at nospam dot iaff106 dot com
27-Sep-2006 08:21
Here is a simple extended version of ParseURL(). 
I needed to make a link that will be saved off site but point to different file
than the one creating the link.

So I needed to get the path without the file name so I could change the
 file name.

Here it is:

<?php
function ParseURLplus($url){
$URLpcs = (parse_url($url));
$PathPcs = explode("/",$URLpcs['path']);
$URLpcs['file'] = end($PathPcs);
unset(
$PathPcs[key($PathPcs)]);
$URLpcs['dir'] = implode("/",$PathPcs);
return (
$URLpcs);
}

$url = 'http://username:password@hostname/path/directory/file.php?arg=
value#anchor'
;

$URLpcs = ParseURLplus($url);

print_r($URLpcs);
?>

Now I can change the $URLpcs['file'] and then glue itback together to make
 a new url.
corgilabs at SPAM_NO_THANK_YOUgmail dot com
14-Jul-2006 03:59
I hope this is helpful! Cheers!
-eo

<?

# Author: Eric O
# Date: July 13, 2006
# Go Zizou!! :O)

# Creating Automatic Self-Redirect To Secure Version
# of Website as Seen on Paypal and other secure sites
# Changes HTTP to HTTPS

#gets the URI of the script
$url $_SERVER['SCRIPT_URI'];

#chops URI into bits BORK BORK BORK
$chopped = parse_url($url);

#HOST and PATH portions of your final destination
$destination = $chopped[host].$chopped[path];

#if you are not HTTPS, then do something about it
if($chopped[scheme] != "https"){

#forwards to HTTP version of URI with secure certificate
header("Location: https://$destination");

exit();

}

?>
php dot net at NOSPAM dot juamei dot com
09-May-2006 08:18
Modfied version of glue_url to avoid error messages if the error_reporting is set high.

function glue_url($parsed)
{
    if (! is_array($parsed)) return false;
        $uri = isset($parsed['scheme']) ? $parsed['scheme'].':'.((strtolower($parsed['scheme']) == 'mailto') ? '':'//'): '';
        $uri .= isset($parsed['user']) ? $parsed['user'].($parsed['pass']? ':'.$parsed['pass']:'').'@':'';
        $uri .= isset($parsed['host']) ? $parsed['host'] : '';
        $uri .= isset($parsed['port']) ? ':'.$parsed['port'] : '';
        $uri .= isset($parsed['path']) ? $parsed['path'] : '';
        $uri .= isset($parsed['query']) ? '?'.$parsed['query'] : '';
        $uri .= isset($parsed['fragment']) ? '#'.$parsed['fragment'] : '';
    return $uri;
}
TheShadow
31-Dec-2004 05:36
You may want to check out the PEAR NET_URL class. It provides easy means to manipulate URL strings.

http://pear.php.net/package/Net_URL
matt at cryptography dot com
10-May-2004 05:36
Modified version of glue_url()
Cox's,Anonimous fucntion

<?php
function glue_url($parsed) {
   if (!
is_array($parsed)) return false;
      
$uri = $parsed['scheme'] ? $parsed['scheme'].':'.((strtolower($parsed['scheme']) == 'mailto') ? '':'//'): '';
      
$uri .= $parsed['user'] ? $parsed['user'].($parsed['pass']? ':'.$parsed['pass']:'').'@':'';
      
$uri .= $parsed['host'] ? $parsed['host'] : '';
      
$uri .= $parsed['port'] ? ':'.$parsed['port'] : '';
      
$uri .= $parsed['path'] ? $parsed['path'] : '';
      
$uri .= $parsed['query'] ? '?'.$parsed['query'] : '';
      
$uri .= $parsed['fragment'] ? '#'.$parsed['fragment'] : '';
  return
$uri;
}
?>

rawurldecode" width="11" height="7"/> <http_build_query
Last updated: Thu, 31 May 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites