Created
December 10, 2017 04:44
-
-
Save OkoyaUsman/34f9d7f783876a7044d71194c562fee2 to your computer and use it in GitHub Desktop.
Get the domain name from a URL for display purposes in PHP
This file contains hidden or 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