Created
October 17, 2016 15:12
-
-
Save andreiglingeanu/5b1438cb6f41e13d0e87203d0f4809ab to your computer and use it in GitHub Desktop.
PHP: Get the domain name from a URL for display purposes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This is PHP function to convert a user-supplied URL to just the domain name, | |
// which I use as the link text. | |
// Remember you still need to use htmlspecialchars() or similar to escape the | |
// result. | |
function url_to_domain($url) | |
{ | |
$host = @parse_url($url, PHP_URL_HOST); | |
// If the URL can't be parsed, use the original URL | |
// Change to "return false" if you don't want that | |
if (!$host) | |
$host = $url; | |
// The "www." prefix isn't really needed if you're just using | |
// this to display the domain to the user | |
if (substr($host, 0, 4) == "www.") | |
$host = substr($host, 4); | |
// You might also want to limit the length if screen space is limited | |
if (strlen($host) > 50) | |
$host = substr($host, 0, 47) . '...'; | |
return $host; | |
} | |
// EXAMPLE USAGE: | |
$url = 'http://www.example.com/path/to/file'; | |
?> | |
<a href="<?= htmlspecialchars($url) ?>"> | |
<?= htmlspecialchars(url_to_domain($url)) ?> | |
</a> | |
OUTPUT: | |
<a href="http://www.example.com/path/to/file">example.com</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment