note 24748 added to function.parse-url

From: Date: Tue, 27 Aug 2002 16:31:51 +0000
Subject: note 24748 added to function.parse-url
Groups: php.notes 
Request: Send a blank email to php-notes+get-35743@lists.php.net to get a copy of this message
Creating canonical URLs from an unparsed URL: function normalize_url($url) {   $parse = parse_url($url);   if (isset($parse['scheme']))     $scheme = strtolower(trim($parse['scheme']));   if (isset($parse['user'])) {     $user = trim($parse['user']);     if (isset($parse['password']))       $password = trim($parse['password']);   }   if (isset($parse['host'])) {     $host = strtolower(trim($parse['host']));     if (preg_match('^-?\d+', $host)) { // if is an int, convert to IPv4       $host = long2ip(intval($host));     }     if (isset($port) { // already an INT if set, not a STRING       /* try to deduce a missing scheme from port */       if (!isset($scheme)) {         if   ($port == 80)           $scheme = 'http';         else if ($port == 21)           $scheme = 'ftp';         else if ($port == 23)           $scheme = 'telnet';         else if ($port == 443)           $scheme = 'https';         else if ($port == 119) //NNTP           $scheme = 'news';         else if ($port == 70)           $scheme = 'gopher';       }       /* remove redondant default port if scheme is specified */       if ($port == 80 && strcmp($scheme, 'http') == 0 ||         $port == 21 && strcmp($scheme, 'ftp') == 0 ||         $port == 23 && strcmp($scheme, 'telnet') == 0 ||         $port == 443 && strcmp($scheme, 'https') == 0 ||         $port == 119 && strcmp($scheme, 'news') == 0 || //NNTP         $port == 70 && strcmp($scheme, 'gopher') == 0)         unset($port);     }     if (!isset($scheme) {       /* try to deduce the scheme from the host name */       if (strncmp($host, 'www.', 4) == 0)         $scheme = 'http';       else if (strncmp($host, 'ftp.', 4) == 0)         $scheme = 'ftp';       else if (strncmp($host, 'gopher.', 7) == 0)         $scheme = 'gopher';       /* else check if this could be an email address (user@host) */       else if (isset($user) && !isset($password)         && !isset($port) && !isset($path))         $scheme = 'mailto';     }   }   if (isset($parse['path'])) {     $path = trim($parse['path']);   }   else if (strcmp($scheme, 'http') == 0     || strcmp($scheme, 'https') == 0     || strcmp($scheme, 'ftp') == 0     || strcmp($scheme, 'gopher') == 0) {       $path = '/';   }   if (isset($parse['query'])) {     $query = $parse['query'];   }   if (isset($parse['fragment']))     $fragment = trim($parse['fragment']);   /* glue the elements */   if (isset($scheme)) {     url = $scheme . ':';     if (strcmp($scheme, 'mailto') != 0 &&       strcmp($scheme, 'telnet') != 0)       url .= '//';   }   if (isset($user)) {     url .= $user;     if (isset($password))       url .= ':' . $password;     url .= '@'   }   if (isset($host)) {     url .= $host;     if (isset($port))       url .= ':' . $port;   }   if (isset($path))     url .= $path;   if (isset($query))     url .= '?' . $query;   if (isset($fragment))     url .= '#' . $fragment;   return $url; } -- http://www.php.net/manual/en/function.parse-url.php http://master.php.net/manage/user-notes.php?action=edit+24748 http://master.php.net/manage/user-notes.php?action=delete+24748 http://master.php.net/manage/user-notes.php?action=reject+24748

« previous php.notes (#35743) next »