Skip to content

Commit

Permalink
update wprs-get_domain
Browse files Browse the repository at this point in the history
  • Loading branch information
iwillhappy1314 authored Aug 2, 2022
1 parent 326975c commit 6c44366
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,23 +370,32 @@ function wprs_get_current_url()
*/
function wprs_get_domain($url)
{
$host = @parse_url($url, PHP_URL_HOST);

if ( ! $host) {
$host = $url;
}

if (strpos($host, 'www.') === 0) {
$host = substr($host, 4);
}
$urlData = parse_url($url);
$urlHost = isset($urlData[ 'host' ]) ? $urlData[ 'host' ] : '';
$isIP = (bool)ip2long($urlHost);
if ($isIP) {
/** To check if it's ip then return same ip */
return $urlHost;
}
/** Add/Edit you TLDs here */
$urlMap = ['com', 'com.pk', 'co.uk', '.com.cn', '.cn'];

$host = '';
$hostData = explode('.', $urlHost);
if (isset($hostData[ 1 ])) {
/** To check "localhost" because it'll be without any TLDs */
$hostData = array_reverse($hostData);

if (in_array($hostData[ 1 ] . '.' . $hostData[ 0 ], $urlMap)) {
$host = $hostData[ 2 ] . '.' . $hostData[ 1 ] . '.' . $hostData[ 0 ];
} elseif (in_array($hostData[ 0 ], $urlMap)) {
$host = $hostData[ 1 ] . '.' . $hostData[ 0 ];
}

if (strlen($host) > 50) {
$host = substr($host, 0, 47) . '...';
return $host;
}

$host = explode('/', $host)[ 0 ];

return $host;
return ((isset($hostData[ 0 ]) && $hostData[ 0 ] != '') ? $hostData[ 0 ] : 'error no domain'); /* You can change this error in future */
}
}

Expand Down

0 comments on commit 6c44366

Please sign in to comment.