note 85547 added to function.parse-url

From: Date: Fri, 05 Sep 2008 07:12:11 +0000
Subject: note 85547 added to function.parse-url
Groups: php.notes 
Request: Send a blank email to php-notes+get-144152@lists.php.net to get a copy of this message
A rehash of code modified by "laulibrius at hotmail dot com". This also parses urls for hosts that don't have a domain name and just use an IP as the hostname. The old code would assume that the IP octets were a subdomain. So the url "http://255.255.255.255/" would return 255.255 as a subdomain of 255.255. <?php parseUrl($url) { $r = "^(?:(?P<scheme>\w+)://)?"; $r .= "(?:(?P<login>\w+):(?P<pass>\w+)@)?"; $ip="(?:[0-9]{1,3}+\.){3}+[0-9]{1,3}";//ip check $s="(?P<subdomain>[-\w\.]+)\.)?";//subdomain $d="(?P<domain>[-\w]+\.)";//domain $e="(?P<extension>\w+)";//extension $r.="(?P<host>(?(?=".$ip.")(?P<ip>".$ip.")|(?:".$s.$d.$e."))"; $r .= "(?::(?P<port>\d+))?"; $r .= "(?P<path>[\w/]*/(?P<file>\w+(?:\.\w+)?)?)?"; $r .= "(?:\?(?P<arg>[\w=&]+))?"; $r .= "(?:#(?P<anchor>\w+))?"; $r = "!$r!"; // Delimiters preg_match($r, $url,$out); } ?> If you need to validate the host IP this is easier than using regex. <?php $parsed=parseUrl($url); if($parsed['ip']) { if(long2ip(ip2long($parsed['ip']))==$parsed['ip']){//validates IP echo $parsed['ip']." is a valid host"; } else { echo $parsed['ip']." is not a valid host"; } } ?> ---- Server IP: 216.235.15.211 Probable Submitter: 69.157.2.162 ---- Manual Page -- http://www.php.net/manual/en/function.parse-url.php Edit -- https://master.php.net/note/edit/85547 Del: integrated -- https://master.php.net/note/delete/85547/integrated Del: useless -- https://master.php.net/note/delete/85547/useless Del: bad code -- https://master.php.net/note/delete/85547/bad+code Del: spam -- https://master.php.net/note/delete/85547/spam Del: non-english -- https://master.php.net/note/delete/85547/non-english Del: in docs -- https://master.php.net/note/delete/85547/in+docs Del: other reasons-- https://master.php.net/note/delete/85547 Reject -- https://master.php.net/note/reject/85547 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144152) next »