note 85276 added to function.parse-url
| From: | marcopanichi at osu1 dot php dot net | Date: | Sat, 23 Aug 2008 08:47:18 +0000 |
| Subject: | note 85276 added to function.parse-url | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143764@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 80.241.173.254
Probable Submitter: 85.18.136.71
----
Manual Page -- http://www.php.net/manual/en/function.parse-url.php
Edit -- https://master.php.net/note/edit/85276
Del: integrated -- https://master.php.net/note/delete/85276/integrated
Del: useless -- https://master.php.net/note/delete/85276/useless
Del: bad code -- https://master.php.net/note/delete/85276/bad+code
Del: spam -- https://master.php.net/note/delete/85276/spam
Del: non-english -- https://master.php.net/note/delete/85276/non-english
Del: in docs -- https://master.php.net/note/delete/85276/in+docs
Del: other reasons-- https://master.php.net/note/delete/85276
Reject -- https://master.php.net/note/reject/85276
Search -- https://master.php.net/manage/user-notes.php