Hướng dẫn php parse current url

(PHP 4, PHP 5, PHP 7, PHP 8)

Show

parse_urlParse a URL and return its components

Description

parse_url(string $url, int $component = -1): int|string|array|null|false

This function is not meant to validate the given URL, it only breaks it up into the parts listed below. Partial and invalid URLs are also accepted, parse_url() tries its best to parse them correctly.

Parameters

url

The URL to parse.

component

Specify one of PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT to retrieve just a specific URL component as a string (except when PHP_URL_PORT is given, in which case the return value will be an int).

Return Values

On seriously malformed URLs, parse_url() may return false.

If the component parameter is omitted, an associative array is returned. At least one element will be present within the array. Potential keys within this array are:

  • scheme - e.g. http
  • host
  • port
  • user
  • pass
  • path
  • query - after the question mark ?
  • fragment - after the hashmark #

If the component parameter is specified, parse_url() returns a string (or an int, in the case of PHP_URL_PORT) instead of an array. If the requested component doesn't exist within the given URL, null will be returned. As of PHP 8.0.0, parse_url() distinguishes absent and empty queries and fragments:

http://example.com/foo → query = null, fragment = null
http://example.com/foo? → query = "",   fragment = null
http://example.com/foo# → query = null, fragment = ""
http://example.com/foo?# → query = "",   fragment = ""

Previously all cases resulted in query and fragment being null.

Note that control characters (cf. ctype_cntrl()) in the components are replaced with underscores (_).

Changelog

VersionDescription
8.0.0 parse_url() will now distinguish absent and empty queries and fragments.

Examples

Example #1 A parse_url() example

$url 'http://username:password@hostname:9090/path?arg=value#anchor';var_dump(parse_url($url));
var_dump(parse_url($urlPHP_URL_SCHEME));
var_dump(parse_url($urlPHP_URL_USER));
var_dump(parse_url($urlPHP_URL_PASS));
var_dump(parse_url($urlPHP_URL_HOST));
var_dump(parse_url($urlPHP_URL_PORT));
var_dump(parse_url($urlPHP_URL_PATH));
var_dump(parse_url($urlPHP_URL_QUERY));
var_dump(parse_url($urlPHP_URL_FRAGMENT));
?>

The above example will output:

array(8) {
  ["scheme"]=>
  string(4) "http"
  ["host"]=>
  string(8) "hostname"
  ["port"]=>
  int(9090)
  ["user"]=>
  string(8) "username"
  ["pass"]=>
  string(8) "password"
  ["path"]=>
  string(5) "/path"
  ["query"]=>
  string(9) "arg=value"
  ["fragment"]=>
  string(6) "anchor"
}
string(4) "http"
string(8) "username"
string(8) "password"
string(8) "hostname"
int(9090)
string(5) "/path"
string(9) "arg=value"
string(6) "anchor"

Example #2 A parse_url() example with missing scheme

$url '//www.example.com/path?googleguy=googley';// Prior to 5.4.7 this would show the path as "//www.example.com/path"
var_dump(parse_url($url));
?>

The above example will output:

array(3) {
  ["host"]=>
  string(15) "www.example.com"
  ["path"]=>
  string(5) "/path"
  ["query"]=>
  string(17) "googleguy=googley"
}

Notes

Caution

This function may not give correct results for relative or invalid URLs, and the results may not even match common behavior of HTTP clients. If URLs from untrusted input need to be parsed, extra validation is required, e.g. by using filter_var() with the FILTER_VALIDATE_URL filter.

Note:

This function is intended specifically for the purpose of parsing URLs and not URIs. However, to comply with PHP's backwards compatibility requirements it makes an exception for the file:// scheme where triple slashes (file:///...) are allowed. For any other scheme this is invalid.

See Also

  • pathinfo() - Returns information about a file path
  • parse_str() - Parses the string into variables
  • http_build_query() - Generate URL-encoded query string
  • dirname() - Returns a parent directory's path
  • basename() - Returns trailing name component of path
  • » RFC 3986

thomas at gielfeldt dot com

10 years ago

[If you haven't yet] been able to find a simple conversion back to string from a parsed url, here's an example:

$url

= 'http://usr::81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment';
if (
$url === unparse_url(parse_url($url))) {
  print
"YES, they match!\n";
}

function

unparse_url($parsed_url) {
 
$scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
 
$host     = isset($parsed_url['host']) ? $parsed_url['host'] : '';
 
$port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
 
$user     = isset($parsed_url['user']) ? $parsed_url['user'] : '';
 
$pass     = isset($parsed_url['pass']) ? ':' . $parsed_url['pass']  : '';
 
$pass     = ($user || $pass) ? "$pass@" : '';
 
$path     = isset($parsed_url['path']) ? $parsed_url['path'] : '';
 
$query    = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
 
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
  return
"$scheme$user$pass$host$port$path$query$fragment";
}
?>

lauris () lauris ! lv

8 years ago

Here is utf-8 compatible parse_url() replacement function based on "laszlo dot janszky at gmail dot com" work. Original incorrectly handled URLs with user:pass. Also made PHP 5.5 compatible (got rid of now deprecated regex /e modifier).

/**
     * UTF-8 aware parse_url() replacement.
     *
     * @return array
     */
   
function mb_parse_url($url)
    {
       
$enc_url = preg_replace_callback(
           
'%[^:/@?&=#]+%usD',
            function (
$matches)
            {
                return
urlencode($matches[0]);
            },
           
$url
       
);$parts = parse_url($enc_url);

                if(

$parts === false)
        {
            throw new \
InvalidArgumentException('Malformed URL: ' . $url);
        }

                foreach(

$parts as $name => $value)
        {
           
$parts[$name] = urldecode($value);
        }

                return

$parts;
    }
?>

mys5droid at gmail dot com

6 years ago

I have coded a function which converts relative URL to absolute URL for a project of mine. Considering I could not find it elsewhere, I figured I would post it here.

The following function takes in 2 parameters, the first parameter is the URL you want to convert from relative to absolute, and the second parameter is a sample of the absolute URL.

Currently it does not resolve '../' in the URL, only because I do not need it. Most webservers will resolve this for you. If you want it to resolve the '../' in the path, it just takes minor modifications.

function relativeToAbsolute($inurl, $absolute) {
   
// Get all parts so not getting them multiple times :)
   
$absolute_parts = parse_url($absolute);   
   
// Test if URL is already absolute (contains host, or begins with '/')
   
if ( (strpos($inurl, $absolute_parts['host']) == false) ) {
       
// Define $tmpurlprefix to prevent errors below
       
$tmpurlprefix = "";
       
// Formulate URL prefix    (SCHEME)                   
       
if (!(empty($absolute_parts['scheme']))) {
           
// Add scheme to tmpurlprefix
           
$tmpurlprefix .= $absolute_parts['scheme'] . "://";
        }
       
// Formulate URL prefix (USER, PASS)   
       
if ((!(empty($absolute_parts['user']))) and (!(empty($absolute_parts['pass'])))) {
           
// Add user:port to tmpurlprefix
           
$tmpurlprefix .= $absolute_parts['user'] . ":" . $absolute_parts['pass'] . "@";   
        }
       
// Formulate URL prefix    (HOST, PORT)   
       
if (!(empty($absolute_parts['host']))) {
           
// Add host to tmpurlprefix
           
$tmpurlprefix .= $absolute_parts['host'];
           
// Check for a port, add if exists
           
if (!(empty($absolute_parts['port']))) {
               
// Add port to tmpurlprefix
               
$tmpurlprefix .= ":" . $absolute_parts['port'];
            }
        }
       
// Formulate URL prefix    (PATH) and only add it if the path to image does not include ./   
       
if ( (!(empty($absolute_parts['path']))) and (substr($inurl, 0, 1) != '/') ) {
           
// Get path parts
           
$path_parts = pathinfo($absolute_parts['path']);
           
// Add path to tmpurlprefix
           
$tmpurlprefix .= $path_parts['dirname'];
           
$tmpurlprefix .= "/";
        }
        else {   
           
$tmpurlprefix .= "/";   
        }   
       
// Lets remove the '/'
       
if (substr($inurl, 0, 1) == '/') { $inurl = substr($inurl, 1); }   
       
// Lets remove the './'
       
if (substr($inurl, 0, 2) == './') { $inurl = substr($inurl, 2); }   
        return
$tmpurlprefix . $inurl;
    }   
    else {
       
// Path is already absolute. Return it :)
       
return $inurl;
    }
}
// Define a sample absolute  URL
$absolute = "http://" . "user::8080/path/to/index.html"; // Just evading php.net spam filter, not sure how example.com is spam...

/* EXAMPLE 1 */

echo relativeToAbsolute($absolute, $absolute) . "\n";
/* EXAMPLE 2 */
echo relativeToAbsolute("img.gif", $absolute) . "\n";
/* EXAMPLE 3 */
echo relativeToAbsolute("/img.gif", $absolute) . "\n";
/* EXAMPLE 4 */
echo relativeToAbsolute("./img.gif", $absolute) . "\n";
/* EXAMPLE 5 */
echo relativeToAbsolute("../img.gif", $absolute) . "\n";
/* EXAMPLE 6 */
echo relativeToAbsolute("images/img.gif", $absolute) . "\n";
/* EXAMPLE 7 */
echo relativeToAbsolute("/images/img.gif", $absolute) . "\n";
/* EXAMPLE 8 */
echo relativeToAbsolute("./images/img.gif", $absolute) . "\n";
/* EXAMPLE 9 */
echo relativeToAbsolute("../images/img.gif", $absolute) . "\n";?>

OUTPUTS:
http :// user::8080/path/to/index.html
http :// user::8080/path/to/img.gif
http :// user::8080/img.gif
http :// user::8080/path/to/img.gif
http :// user::8080/path/to/../img.gif
http :// user::8080/path/to/images/img.gif
http :// user::8080/images/img.gif
http :// user::8080/path/to/images/img.gif
http :// user::8080/path/to/../images/img.gif

Sorry if the above code is not your style, or if you see it as "messy" or you think there is a better way to do it. I removed as much of the white space as possible.

Improvements are welcome :)

simbappo

2 years ago

unset a query var from passed in or current URL:

function unsetqueryvar($var, $url=null) {
    if (null == $url) $url = $_SERVER['REQUEST_URI'];
    //mogrify to list
    $url = parse_url($url);
    $rq = [];
    parse_str($url['query'], $rq);
    unset($rq[$var]);
    return $url['scheme'].$url['host'].$url['path'].'?'.http_build_query($rq).$url['fragment'];
}

jerome at chaman dot ca

7 years ago

It may be worth reminding that the value of the #fragment never gets sent to the server.  Anchors processing is exclusively client-side.

adrian-php at sixfingeredman dot net

15 years ago

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).

/**
* 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);
}
?>

pjpawel

5 months ago

Unfortunately parse_url() DO NOT parse correctly urls without scheme or '//'. For example 'www.xyz.com' is consider as path not host:

Code:
var_dump(parse_url('www.xyz.com'));
?>
Output:
array(1) {
["path"]=>
string(10) "www.xyz.com"
}

To get better output change url to:
'//www.xyz.com' or 'http://www.xyz.com'

vbrazas15 at gmail dot com

1 year ago

My hamble improvements to the famouse `unparse_url` function by "thomas at gielfeldt dot com":

```php
    /**
     * @param array $parsedUrl -- result of the library `parse_url()` function
     *
     * @return string
     */
    function unparseUrl(array $parsedUrl): string
    {
        // PHP_URL_SCHEME
        $scheme = empty($parsedUrl[PHP_URL_SCHEME]) ? '' : (rtrim($parsedUrl['scheme'], ':/') . '://');

        $user = empty($parsedUrl[PHP_URL_USER]) ? '' : rtrim($parsedUrl['user'], '@:');
        $pass = empty($parsedUrl[PHP_URL_PASS]) ? '' : (':' . trim($parsedUrl['pass'], '@:'));

        $pass = !$user ? '' : ($pass . '@');

        $host = empty($parsedUrl[PHP_URL_HOST]) ? '' : rtrim($parsedUrl['host'], '/');
        $port = empty($parsedUrl[PHP_URL_PORT]) ? '' : (':' . (int)ltrim($parsedUrl['port'], ':'));
        $path = empty($parsedUrl[PHP_URL_PATH]) ? '' : ('/' . ltrim($parsedUrl['path'], '/'));

        $host = ($host && !$port && !$path) ? $parsedUrl['host'] : $host;
        $path = ($path && !$host && !$port) ? $parsedUrl['path'] : $path;

        $query = empty($parsedUrl[PHP_URL_QUERY]) ? '' : ('?' . ltrim($parsedUrl['query'], '?'));
        $fragment = empty($parsedUrl[PHP_URL_FRAGMENT]) ? '' : ('#' . ltrim($parsedUrl['fragment'], '#'));

        return "$scheme$user$pass$host$port$path$query$fragment";
    }
```

vdklah at hotmail dot com

13 years ago

Some example that determines the URL port.
When port not specified, it derives it from the scheme.

function getUrlPort( $urlInfo )
{
    if( isset(
$urlInfo['port']) ) {
       
$port = $urlInfo['port'];
    } else {
// no port specified; get default port
       
if (isset($urlInfo['scheme']) ) {
            switch(
$urlInfo['scheme'] ) {
                case
'http':
                   
$port = 80; // default for http
                   
break;
                case
'https':
                   
$port = 443; // default for https
                   
break;
                case
'ftp':
                   
$port = 21; // default for ftp
                   
break;
                case
'ftps':
                   
$port = 990; // default for ftps
                   
break;
                default:
                   
$port = 0; // error; unsupported scheme
                   
break;
            }
        } else {
           
$port = 0; // error; unknown scheme
       
}
    }
    return
$port;
}
$url = "http://nl3.php.net/manual/en/function.parse-url.php";
$urlInfo = parse_url( $url );
$urlPort = getUrlPort( $urlInfo );
if(
$urlPort !== 0 ) {
    print
'Found URL port: '.$urlPort;
} else {
    print
'ERROR: Could not find port at URL: '.$url;
}
?>

to1ne at hotmail dot com

14 years ago

Based on the idea of "jbr at ya-right dot com" have I been working on a new function to parse the url:

function parseUrl($url) {
   
$r  = "^(?:(?P\w+)://)?";
   
$r .= "(?:(?P\w+):(?P\w+)@)?";
   
$r .= "(?P(?:(?P[\w\.]+)\.)?" . "(?P\w+\.(?P\w+)))";
   
$r .= "(?::(?P\d+))?";
   
$r .= "(?P[\w/]*/(?P\w+(?:\.\w+)?)?)?";
   
$r .= "(?:\?(?P[\w=&]+))?";
   
$r .= "(?:#(?P\w+))?";
   
$r = "!$r!";                                                // Delimiterspreg_match ( $r, $url, $out );

        return

$out;
}
print_r ( parseUrl ( 'me::29000/pear/validate.html?happy=me&sad=you#url' ) );
?>

This returns:
Array
(
    [0] => me::29000/pear/validate.html?happy=me&sad=you#url
    [scheme] =>
    [1] =>
    [login] => me
    [2] => me
    [pass] => you
    [3] => you
    [host] => sub.site.org
    [4] => sub.site.org
    [subdomain] => sub
    [5] => sub
    boxhoidap.com => site.org
    [6] => site.org
    [extension] => org
    [7] => org
    [port] => 29000
    [8] => 29000
    [path] => /pear/validate.html
    [9] => /pear/validate.html
    [file] => validate.html
    [10] => validate.html
    [arg] => happy=me&sad=you
    [11] => happy=me&sad=you
    [anchor] => url
    [12] => url
)

So both named and numbered array keys are possible.

It's quite advanced, but I think it works in any case... Let me know if it doesn't...

ivijan dot stefan at gmail dot com

8 years ago

Here's a good way to using parse_url () gets the youtube link.
This function I used in many works:

function youtube($url, $width=560, $height=315, $fullscreen=true)
{
   
parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
   
$youtube= '';
    return
$youtube;
}
// show youtube on my page
$url='http://www.youtube.com/watch?v=yvTd6XxgCBE';
youtube($url, 560, 315, true);
?>

parse_url () allocates a unique youtube code and  put into iframe link and displayed on your page. The size of the videos choose yourself.

Enjoy.

kibblewhite+php at live dot com

1 year ago

This function 'parse_rebuild_url' will parse and reassemble your URL with new values provided by the 'overwrite_parsed_url_array' back together.
It is also possible to overwrite the URL components by key name and to merge or overwrite query parameters.

    $test_url

= 'http://usr::81/mypath/myfile.html?a=b&b[]=2&b[]=3&z=9#myfragment';$new_url_01_overwrite_query_params = parse_rebuild_url( $test_url, array(
       
'host' => 'new-hostname.tld',
       
'query' => array(
           
'test' => 'Hello World',
           
'a'    => array( 'c', 'd' ),
           
'z'    => 8
       
),
       
'fragment' => 'new-fragment-value'
   
), false );$new_url_02_mergewith_query_params = parse_rebuild_url( $test_url, array(
       
'query' => array(
           
'test' => 'Hello World',
           
'a'    => array( 'c', 'd' ),
           
'z'     => 8
       
),
       
'fragment' => 'new-fragment-value'
   
), true );

    function

parse_rebuild_url( $url, $overwrite_parsed_url_array, $merge_query_parameters = true ) {$parsed_url_array = parse_url( $url );
       
$parsed_url_keys_array = array(
           
'scheme'        => null,
           
'abempty'       => isset( $parsed_url_array['scheme'] ) ? '://' : null,
           
'user'          => null,
           
'authcolon'     => isset( $parsed_url_array['pass'] ) ? ':' : null,
           
'pass'          => null,
           
'authat'        => isset( $parsed_url_array['user'] ) ? '@' : null,
           
'host'          => null,
           
'portcolon'     => isset( $parsed_url_array['port'] ) ? ':' : null,
           
'port'          => null,
           
'path'          => null,
           
'param'         => isset( $parsed_url_array['query'] ) ? '?' : null,
           
'query'         => null,
           
'hash'          => isset( $parsed_url_array['fragment'] ) ? '#' : null,
           
'fragment'      => null
       
);

        if ( isset(

$parsed_url_array['query'] ) && $merge_query_parameters === true ) {
           
parse_str( $parsed_url_array['query'], $query_array );
           
$overwrite_parsed_url_array['query'] = array_merge_recursive( $query_array, $overwrite_parsed_url_array['query'] );
        }
$query_parameters = http_build_query( $overwrite_parsed_url_array['query'], null, '&', PHP_QUERY_RFC1738 );
       
$overwrite_parsed_url_array['query'] = urldecode( preg_replace( '/%5B[0-9]+%5D/simU', '%5B%5D', $query_parameters ) );$fully_parsed_url_array = array_filter( array_merge( $parsed_url_keys_array, $parsed_url_array, $overwrite_parsed_url_array ) );
        return
implode( null, $fully_parsed_url_array );

    }

demerit

5 years ago

There is a change in PHP 7 (I noticed it in 7.1 upgrading from 5.3) where if the password portion has an octothorpe (#) in it, parsing fails in 7.1, whereas it succeeds in 5.3.

therselman at gmail

10 years ago

UTF-8 aware parse_url() replacement.

I've realized that even though UTF-8 characters are not allowed in URL's, I have to work with a lot of them and parse_url() will break.

Based largely on the work of "mallluhuct at gmail dot com", I added parse_url() compatible "named values" which makes the array values a lot easier to work with (instead of just numbers). I also implemented detection of port, username/password and a back-reference to better detect URL's like this: //en.wikipedia.com
... which, although is technically an invalid URL, it's used extensively on sites like wikipedia in the href of anchor tags where it's valid in browsers (one of the types of URL's you have to support when crawling pages). This will be accurately detected as the host name instead of "path" as in all other examples.

I will submit my complete function (instead of just the RegExp) which is an almost "drop-in" replacement for parse_url(). It returns a cleaned up array (or false) with values compatible with parse_url(). I could have told the preg_match() not to store the unused extra values, but it would complicate the RegExp and make it more difficult to read, understand and extend. The key to detecting UTF-8 characters is the use of the "u" parameter in preg_match().

function parse_utf8_url($url)
{
    static
$keys = array('scheme'=>0,'user'=>0,'pass'=>0,'host'=>0,'port'=>0,'path'=>0,'query'=>0,'fragment'=>0);
    if (
is_string($url) && preg_match(
           
'~^((?P[^:/?#]+):(//))?((\\3|//)?(?:(?P[^:]+):(?P[^@]+)@)?(?P[^/?:#]*))(:(?P\\d+))?' .
           
'(?P[^?#]*)(\\?(?P[^#]*))?(#(?P.*))?~u', $url, $matches))
    {
        foreach (
$matches as $key => $value)
            if (!isset(
$keys[$key]) || empty($value))
                unset(
$matches[$key]);
        return
$matches;
    }
    return
false;
}
?>

UTF-8 URL's can/should be "normalized" after extraction with this function.

laszlo dot janszky at gmail dot com

10 years ago

Created another parse_url utf-8 compatible function.
function mb_parse_url($url) {
   
$encodedUrl = preg_replace('%[^:/?#&=\.]+%usDe', 'urlencode(\'$0\')', $url);
   
$components = parse_url($encodedUrl);
    foreach (
$components as &$component)
       
$component = urldecode($component);
    return
$components;
}
?>

ap dot public1 at gmail dot com

13 years ago

Simple static library that allows easy manipulation of url parameters:

    /**
     * File provides easy way to manipulate url parameters
     * @author Alexander Podgorny
     */
class Url {
       
/**
         * Splits url into array of it's pieces as follows:
         * [scheme]://[user]:[pass]@[host]/[path]?[query]#[fragment]
         * In addition it adds 'query_params' key which contains array of
         * url-decoded key-value pairs
         *
         * @param String $sUrl Url
         * @return Array Parsed url pieces
         */
       
public static function explode($sUrl) {
           
$aUrl = parse_url($sUrl);
           
$aUrl['query_params'] = array();
           
$aPairs = explode('&', $aUrl['query']);
           
DU::show($aPairs);
            foreach(
$aPairs as $sPair) {
                if (
trim($sPair) == '') { continue; }
                list(
$sKey, $sValue) = explode('=', $sPair);
               
$aUrl['query_params'][$sKey] = urldecode($sValue);
            }
            return
$aUrl;
        }
       
/**
         * Compiles url out of array of it's pieces (returned by explodeUrl)
         * 'query' is ignored if 'query_params' is present
         *
         * @param Array $aUrl Array of url pieces
         */
       
public static function implode($aUrl) {
           
//[scheme]://[user]:[pass]@[host]/[path]?[query]#[fragment]$sQuery = '';// Compile query
           
if (isset($aUrl['query_params']) && is_array($aUrl['query_params'])) {
               
$aPairs = array();
                foreach (
$aUrl['query_params'] as $sKey=>$sValue) {
                   
$aPairs[] = $sKey.'='.urlencode($sValue);              
                }
               
$sQuery = implode('&', $aPairs);   
            } else {
               
$sQuery = $aUrl['query'];
            }
// Compile url
           
$sUrl =
               
$aUrl['scheme'] . '://' . (
                    isset(
$aUrl['user']) && $aUrl['user'] != '' && isset($aUrl['pass'])
                       ?
$aUrl['user'] . ':' . $aUrl['pass'] . '@'
                      
: ''
               
) .
               
$aUrl['host'] . (
                    isset(
$aUrl['path']) && $aUrl['path'] != ''
                      
? $aUrl['path']
                       :
''
               
) . (
                  
$sQuery != ''
                      
? '?' . $sQuery
                      
: ''
               
) . (
                   isset(
$aUrl['fragment']) && $aUrl['fragment'] != ''
                      
? '#' . $aUrl['fragment']
                       :
''
               
);
            return
$sUrl;
        }
       
/**
         * Parses url and returns array of key-value pairs of url params
         *
         * @param String $sUrl
         * @return Array
         */
       
public static function getParams($sUrl) {
           
$aUrl = self::explode($sUrl);
            return
$aUrl['query_params'];
        }
       
/**
         * Removes existing url params and sets them to those specified in $aParams
         *
         * @param String $sUrl Url
         * @param Array $aParams Array of Key-Value pairs to set url params to
         * @return  String Newly compiled url
         */
       
public static function setParams($sUrl, $aParams) {
           
$aUrl = self::explode($sUrl);
           
$aUrl['query'] = '';
           
$aUrl['query_params'] = $aParams;
            return
self::implode($aUrl);
        }
       
/**
         * Updates values of existing url params and/or adds (if not set) those specified in $aParams
         *
         * @param String $sUrl Url
         * @param Array $aParams Array of Key-Value pairs to set url params to
         * @return  String Newly compiled url
         */
       
public static function updateParams($sUrl, $aParams) {
           
$aUrl = self::explode($sUrl);
           
$aUrl['query'] = '';
           
$aUrl['query_params'] = array_merge($aUrl['query_params'], $aParams);
            return
self::implode($aUrl);
        }
    }
?>

zappascripts at gmail com

5 years ago

Here's a simple class I made that makes use of this parse_url.
I needed a way for a page to retain get parameters but also edit or add onto them.
I also had some pages that needed the same GET paramaters so I also added a way to change the path.

class Paths{

    private

$url;
    public function
__construct($url){
       
$this->url = parse_url($url);
    }

        public function

returnUrl(){
       
$return = $this->url['path'].'?'.$this->url['query'];
       
$return = (substr($return,-1) == "&")? substr($return,0,-1) : $return;
       
$this->resetQuery();
        return
$return;
    }

        public function

changePath($path){
       
$this->url['path'] = $path;
    }

        public function

editQuery($get,$value){
       
$parts = explode("&",$this->url['query']);
       
$return = "";
        foreach(
$parts as $p){
           
$paramData = explode("=",$p);
            if(
$paramData[0] == $get){
               
$paramData[1] = $value;
            }
           
$return .= implode("=",$paramData).'&';

                    }

$this->url['query'] = $return;
    }

        public function

addQuery($get,$value){
       
$part = $get."=".$value;
       
$and = ($this->url['query'] == "?") ? "" : "&";
       
$this->url['query'] .= $and.$part;
    }

        public function

checkQuery($get){
       
$parts = explode("&",$this->url['query']);

                    foreach(

$parts as $p){
               
$paramData = explode("=",$p);
                if(
$paramData[0] == $get)
                    return
true;
            }
            return
false;

            }

        public function

buildQuery($get,$value){
        if(
$this->checkQuery($get))
           
$this->editQuery($get,$value);
        else
           
$this->addQuery($get,$value);

            }

        public function

resetQuery(){
       
$this->url = parse_url($_SERVER['REQUEST_URI']);
    }

            }

?>

Useage:

Test.php?foo=1:

$path = new Paths($_SERVER['REQUEST_URI']);
$path->changePath("/baz.php");
$path->buildQuery("foo",2);
$path->buildQuery("bar",3);
echo
$path->returnUrl();
?>

returns: /baz.php?foo=2&bar=3

Hope this is of some use to someone!

utilmind

9 years ago

parse_url doesn't works if the protocol doesn't specified. This seems like sandard, even the youtube doesn't gives the protocol name when generates code for embedding which have a look like "//youtube.com/etc".

So, to avoid bug, you must always check, whether the provided url has the protocol, and if not (starts with 2 slashes) -- add the "http:" prefix.

Rob

7 years ago

I've been working on a generic class that would make URI parsing / building a little easier.

The composer package is here: https://packagist.org/packages/enrise/urihelper

And the repository is here: https://github.com/Enrise/UriHelper

An example of the usage:

$uri = new \Enrise\Uri('http://usr::81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment');
echo
$uri->getScheme(); // http
echo $uri->getUser(); // usr
echo $uri->getPass(); // pss
echo $uri->getHost(); // example.com
echo $uri->getPort(); // 81
echo $uri->getPath(); // /mypath/myfile.html
echo $uri->getQuery(); // a=b&b[]=2&b[]=3
echo $uri->getFragment(); // myfragment
echo $uri->isSchemeless(); // false
echo $uri->isRelative(); // false$uri->setScheme('scheme:child:scheme.VALIDscheme123:');
$uri->setPort(null);

echo

$uri->getUri(); //scheme:child:scheme.VALIDscheme123:usr:/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment
?>

spam at paulisageek dot com

15 years ago

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);
}

nirazuelos at gmail dot com

12 years ago

Hello, for some odd reason, parse_url returns the host (ex. example.com) as the path when no scheme is provided in the input url. So I've written a quick function to get the real host:

function getHost($Address) {
  
$parseUrl = parse_url(trim($Address));
   return
trim($parseUrl[host] ? $parseUrl[host] : array_shift(explode('/', $parseUrl[path], 2)));
}
getHost("example.com"); // Gives example.com
getHost("http://example.com"); // Gives example.com
getHost("www.example.com"); // Gives www.example.com
getHost("http://example.com/xyz"); // Gives example.com
?>

You could try anything! It gives the host (including the subdomain if exists).

Hope it helped you.

need_sunny at yahoo dot com

12 years ago

Thanks to xellisx for his parse_query function. I used it in one of my projects and it works well. But it has an error. I fixed the error and improved it a little bit. Here is my version of it:

// Originally written by xellisx
function parse_query($var)
{
 
/**
   *  Use this function to parse out the query array element from
   *  the output of parse_url().
   */
 
$var  = parse_url($var, PHP_URL_QUERY);
 
$var  = html_entity_decode($var);
 
$var  = explode('&', $var);
 
$arr  = array();

  foreach(

$var as $val)
   {
   
$x          = explode('=', $val);
   
$arr[$x[0]] = $x[1];
   }
  unset(
$val, $x, $var);
  return
$arr;
}
?>

At the first line there was parse_query($val), I made it $var. It used to return a null array before this fix.

I have added the parse_url line. So now the function will only focus in the query part, not the whole URL. This is useful if something like below is done:
$my_GET = parse_query($_SERVER['REQUEST_URI']);
?>

jesse at example dot com

12 years ago

@ solenoid: Your code was very helpful, but it fails when the current URL has no query string (it appends '&' instead of '?' before the query).  Below is a fixed version that catches this edge case and corrects it.

function modify_url($mod)
{
   
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
   
$query = explode("&", $_SERVER['QUERY_STRING']);
    if (!
$_SERVER['QUERY_STRING']) {$queryStart = "?";} else {$queryStart = "&";}
   
// modify/delete data
   
foreach($query as $q)
    {
        list(
$key, $value) = explode("=", $q);
        if(
array_key_exists($key, $mod))
        {
            if(
$mod[$key])
            {
               
$url = preg_replace('/'.$key.'='.$value.'/', $key.'='.$mod[$key], $url);
            }
            else
            {
               
$url = preg_replace('/&?'.$key.'='.$value.'/', '', $url);
            }
        }
    }
   
// add new data
   
foreach($mod as $key => $value)
    {
        if(
$value && !preg_match('/'.$key.'=/', $url))
        {
           
$url .= $queryStart.$key.'='.$value;
        }
    }
    return
$url;
}
?>

bramg dot net1 at gmail dot com

4 years ago

this is my 404 error page is this ok  or it need improvements

/**
* 404.php
*
* The template for displaying 404 pages (not found)
*
* @author    BetterStudio
* @package   Publisher
* @version   2.0.2
*/
get_header();// Shows breadcrumb
if ( publisher_show_breadcrumb() ) {
   
Better_Framework()->breadcrumb()->generate( array(
       
'before'       => '

',
       
'after'        => '
',
       
'custom_class' => 'bc-top-style'
   
) );
}
?>
   

       
( 'content', '' ); ?>>

           


               

                   

                       

                           


                               

404


                           

                           


                               

( '404_not_found' ); ?>


                               

( '404_not_found_message' ); ?>


                               

                           

                       

                       


                           

                               

                                    (); ?>
                               

                           

                       

                   

               


           

       


   

(); ?>

https://bramg.net

solenoid at example dot com

12 years ago

Here's a piece of code that modifies, replaces or removes the url query. This can typically used in paging situations where there are more parameters than the page.

function modify_url($mod)
{
   
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
   
$query = explode("&", $_SERVER['QUERY_STRING']);
   
// modify/delete data
   
foreach($query as $q)
    {
        list(
$key, $value) = explode("=", $q);
        if(
array_key_exists($key, $mod))
        {
            if(
$mod[$key])
            {
               
$url = preg_replace('/'.$key.'='.$value.'/', $key.'='.$mod[$key], $url);
            }
            else
            {
               
$url = preg_replace('/&?'.$key.'='.$value.'/', '', $url);
            }
        }
    }
   
// add new data
   
foreach($mod as $key => $value)
    {
        if(
$value && !preg_match('/'.$key.'=/', $url))
        {
           
$url .= '&'.$key.'='.$value;
        }
    }
    return
$url;
}
// page url: "http://www.example.com/page.php?p=5&show=list&style=23" $url = modify_url(array('p' => 4, 'show' => 'column')); // $url = "http://www.example.com/page.php?p=4&show=column&style=23"
?>

admin at griefer1999 dot uhostfull dot com

7 years ago

function url_parse($url){
$sflfdfldf=$url;
if(
strpos($url,"?")>-1){
$a=explode("?",$url,2);
$url=$a[0];
$query=$a[1];
}
if(
strpos($url,"://")>-1){
$scheme=substr($url,0,strpos($url,"//")-1);
$url=substr($url,strpos($url,"//")+2,strlen($url));
}
if(
strpos($url,"/")>-1){
$a=explode("/",$url,2);
$url=$a[0];
$path="/".$a[1];
}
if(
strpos($url,":")>-1){
$a=explode(":",$url,2);
$url=$a[0];
$port=$a[1];
}
$host=$url;
$url=null;
foreach(array(
"url","scheme","host","port","path","query") as $var){
if(!empty($
$var)){
$return[$var]=$$var;
}
}
//return array("url"=>$sflfdfldf,"scheme"=>$scheme,"host"=>$host,"port"=>$port,"path"=>$path,"query"=>$query,"a"=>$url);
return $return;
}
?>

/* Compare two outputs */
//mine
print_r(url_parse("http://login.yahoo.com?.src=ym&.intl=gb&.lang=zh-Hans-HK&.done=https://mail.yahoo.com"));
//internal
print_r(parse_url("http://login.yahoo.com?.src=ym&.intl=gb&.lang=zh-Hans-HK&.done=https://mail.yahoo.com"));
?>

xellisx

13 years ago

I need to parse out the query string from the referrer, so I created this function.

function parse_query($val)
{
 
/**
   *  Use this function to parse out the query array element from
   *  the output of parse_url().
   */
 
$var  = html_entity_decode($var);
 
$var  = explode('&', $var);
 
$arr  = array();

  foreach(

$var as $val)
   {
   
$x          = explode('=', $val);
   
$arr[$x[0]] = $x[1];
   }
  unset(
$val, $x, $var);
  return
$arr;
}
?>

boctulus @ gmail co!m

2 years ago

parse_url() does not parse some obvious errors so I made a complementary function

function url_check(string $url){
        $sym = null;

            $len = strlen($url);
        for ($i=0; $i<$len; $i++){
            if ($url[$i] == '?'){
                if ($sym == '?' || $sym == '&')
                    return false;

                    $sym = '?';
            }elseif ($url[$i] == '&'){
                if ($sym === null)
                    return false;

                    $sym = '&';
            }
        }
        return true;
    }
}

Anonymous

4 years ago

This function will attempt to parse relative URLs but relaying on it can produce unexpected behavior that can cause some hard to track bugs. (The following results are obtained from PHP 5.5.19)

Attempting to parse a url like this
http://example.com/entities/GOA:98/?search=8989157d1f22
Correctly produces
array (
 
'scheme' => 'http',
 
'host' => 'example.com',
 
'path' => '/entities/GOA:98/',
 
'query' => 'search=8989157d1f22',
);
?>

However, Attempting to parse the relative URL
entities/GOA:98/?search=8989157d1f22
array (
 
'host' => 'entities',
 
'port' => 98,
 
'path' => '/GOA:98/',
 
'query' => 'search=8989157d1f22',
)
?>
If I change :98 to :A98 parse_url parses the URL correctly as
array (
 
'path' => 'entities/GOA:A98/',
 
'query' => 'search=8989157d1f22',
)
?>
Bottom line, Avoid using parse_url for relative urls unless you have tested the expected input and you know parse_url will handle them well.

https://forums.hawacastle.com/

Anonymous

4 years ago

Hello, for some odd reason, parse_url returns the host (ex. example.com) as the path when no scheme is provided in the input url. So I've written a quick function to get the real host:

function getHost($Address) {
  
$parseUrl = parse_url(trim($Address));
   return
trim($parseUrl[host] ? $parseUrl[host] : array_shift(explode('/', $parseUrl[path], 2)));
}
getHost("example.com"); // Gives example.com
getHost("http://example.com"); // Gives example.com
getHost("www.example.com"); // Gives www.example.com
getHost("http://example.com/xyz"); // Gives example.com
?>

You could try anything! It gives the host (including the subdomain if exists).

Hope it helped you.
https://vb.3dlat.com/

Anonymous

4 years ago

To get the params (url query) as Associative array, use this function:

/**
* Returns the url query as associative array
*
* @param    string    query
* @return    array    params
*/
function convertUrlQuery($query) {
   
$queryParts = explode('&', $query); $params = array();
    foreach (
$queryParts as $param) {
       
$item = explode('=', $param);
       
$params[$item[0]] = $item[1];
    }

        return

$params;
}
?>

RobertVow

11 months ago

Đổi thẻ cào sang tiền mặt, doi the cao thanh tien mat uy tín giá rẻ nhanh gọn rút tiền về atm và ví điện tử tự động 24/7,hỗ trợ hướng dẫn tích hợp api siêu ...
Doithenap.com - Đổi thẻ điện thoại thành tiền, Đổi thẻ cào thành ...https://doithenap.com