The best way to parse out the port number from a URL (e.g. 8080 from http://example.com:8080) is to use regular expressions. The PHP function parse_url() will not return port numbers that are specified in the URL. However, I’ve found parse_url() to be useful for retrieving the host name from a URL.
Here’s the regular expression I wrote for getting a port number from the URL — if it has a port number specified.
if (preg_match("/(http:\/\/)([A-Za-z0-9-.]*)(:)([0-9]{1,5})*/i",$url,$matches))
$port = $matches[3];