note 85276 deleted from function.parse-url by cmb
| From: | cmb@php.net | Date: | Sun, 08 Sep 2019 10:34:28 +0000 |
| Subject: | note 85276 deleted from function.parse-url by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212151@lists.php.net to get a copy of this message | ||
Note Submitter: marco panichi
----
my function catch the url written on the browser by the user and does the same thing of parse_url.
but better, I think. I don't like parse_url because it says nothing about elements that it
doesn't find in the url. my function instead return an empty string.
<?php
function get_url()
{
$arr = array();
$uri = $_SERVER['REQUEST_URI'];
// query
$x = array_pad( explode( '?', $uri ), 2, false );
$arr['query'] = ( $x[1] )? $x[1] : '' ;
// resource
$x = array_pad( explode( '/', $x[0] ), 2, false );
$x_last = array_pop( $x );
if( strpos( $x_last, '.' ) === false )
{
$arr['resource'] = '';
$x[] = $x_last;
}
else
{
$arr['resource'] = $x_last;
}
// path
$arr['path'] = implode( '/', $x );
if( substr( $arr['path'], -1 ) !== '/' ) $arr['path'] .=
'/';
// domain
$arr['domain'] = $_SERVER['SERVER_NAME'];
// scheme
$server_prt = explode( '/', $_SERVER['SERVER_PROTOCOL'] );
$arr['scheme'] = strtolower( $server_prt[0] );
// url
$arr['url'] = $arr['scheme'].'://'.$arr['domain'].$uri;
return $arr;
}
?>
PS: I found working with explode is faster than using preg_match (I tryed with getmicrotime function
and 'for' cycles).
PPS: I used array_pad to prevent any notice.