Tuesday, February 4, 2014

PHP: Get Url Components

Example 1: Code

<?php
$url = 'http://me:you@sub.site.org:29000/pear/validate.html?happy=me&sad=you#url';

$output = parse_url($url);
echo '<pre>' . print_r(parse_url($url), true) . '</pre>';
echo 'Path: '.$output['path'];
?>


Example 1: Output
Array
(
[scheme] => http
[host] => sub.site.org
[port] => 29000
[user] => me
[pass] => you
[path] => /pear/validate.html
[query] => happy=me&sad=you
[fragment] => url
)
Path: /pear/validate.html

Example 2:
To retrieve just a specific URL component as a string:
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

Code

<?php
$url = 'http://me:you@sub.site.org:29000/pear/validate.html?happy=me&sad=you#url';

echo parse_url($url, PHP_URL_PATH);
?>


Output

/pear/validate.html


Example 3: Code

<?php
function parseUrl ( $url )
{
$r = '!(?:(\w+)://)?(?:(\w+)\:(\w+)@)?([^/:]+)?';
$r .= '(?:\:(\d*))?([^#?]+)?(?:\?([^#]+))?(?:#(.+$))?!i';

preg_match ( $r, $url, $out );

return $out;
}
$output = parseUrl($url);
echo '<pre>' . print_r($output, true) . '</pre>';
echo 'Path: '.$output[6];
?>


Example 3: Output
Array
(
[0] => http://me:you@sub.site.org:29000/pear/validate.html?happy=me&sad=you#url
[1] => http
[2] => me
[3] => you
[4] => sub.site.org
[5] => 29000
[6] => /pear/validate.html
[7] => happy=me&sad=you
[8] => url
)
Path: /pear/validate.html

Example 3: Explain output

$output[0] = full url
$output[1] = scheme or '' if no scheme was found
$output[2] = username or '' if no auth username was found
$output[3] = password or '' if no auth password was found
$output[4] = domain name or '' if no domain name was found
$output[5] = port number or '' if no port number was found
$output[6] = path or '' if no path was found
$output[7] = query or '' if no query was found
$output[8] = fragment or '' if no fragment was found


Example 4: Code

<?php
$url = 'http://me:you@sub.site.org:29000/pear/validate.html?happy=me&sad=you#url';

echo 'SERVER_PROTOCOL: '.$_SERVER['SERVER_PROTOCOL'];
echo 'SERVER_NAME: '.$_SERVER['SERVER_NAME'];
echo 'SERVER_PORT: '.$_SERVER['SERVER_PORT'];
echo 'PHP_SELF: '.$_SERVER['PHP_SELF'];
echo 'REQUEST_URI: '.$_SERVER['REQUEST_URI'];
?>


Example 4: Output

SERVER_PROTOCOL: HTTP/1.1
SERVER_NAME: sub.site.org
SERVER_PORT: 29000
PHP_SELF: /pear/validate.html
REQUEST_URI: /pear/validate.html?happy=me&sad=you#url


Example 5: Code
function getcurrentpath()
{   $curPageURL = "";
    if ($_SERVER["HTTPS"] != "on")
            $curPageURL .= "http://";
     else
        $curPageURL .= "https://" ;
    if ($_SERVER["SERVER_PORT"] == "80")
        $curPageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     else
        $curPageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        $count = strlen(basename($curPageURL));
        $path = substr($curPageURL,0, -$count);
    return $path ;
}
 
echo getcurrentpath();


Example 5: Output

If current URL is:
http://www.abc.com/file/demo1/contact.html

Output will be:
http://www.abc.com/file/demo1/

Refer: http://blog.vivekv.com/php-get-the-current-url-path.html

No comments:

Post a Comment