Skip to content

Instantly share code, notes, and snippets.

@hernandesbsousa
Last active January 2, 2016 11:49
Show Gist options
  • Select an option

  • Save hernandesbsousa/8299582 to your computer and use it in GitHub Desktop.

Select an option

Save hernandesbsousa/8299582 to your computer and use it in GitHub Desktop.
Removing port from URL in PHP
<?php
$url = 'http://www.example.com:8080/index.html';
$newurl = remove_port(parse_url($url));
echo $newurl;
function remove_port($parsed_url) {
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$port = '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
return "$scheme$host$port$path$query$fragment";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment