Back to PunycodeHelper class

Method urlToUTF8

public static string
urlToUTF8
(mixed $uri)
Transforms a Punycode URL to a UTF-8 URL
Parameters
  • string $uri The Punycode URL to transform
Returns
  • string The UTF-8 URL
Since
  • 3.1.2

Method urlToUTF8 - Source code

/**
 * Transforms a Punycode URL to a UTF-8 URL
 *
 * @param   string  $uri  The Punycode URL to transform
 *
 * @return  string  The UTF-8 URL
 *
 * @since   3.1.2
 */
public static function urlToUTF8($uri)
{
    if (empty($uri)) {
        return '';
    }
    $parsed = UriHelper::parse_url($uri);
    if (!isset($parsed['host']) || $parsed['host'] == '') {
        // If there is no host we do not need to convert it.
        return $uri;
    }
    $host = $parsed['host'];
    $hostExploded = explode('.', $host);
    $newhost = '';
    foreach ($hostExploded as $hostex) {
        $hostex = self::fromPunycode($hostex);
        $newhost .= $hostex . '.';
    }
    $newhost = substr($newhost, 0, -1);
    $newuri = '';
    if (!empty($parsed['scheme'])) {
        // Assume :// is required although it is not always.
        $newuri .= $parsed['scheme'] . '://';
    }
    if (!empty($newhost)) {
        $newuri .= $newhost;
    }
    if (!empty($parsed['port'])) {
        $newuri .= ':' . $parsed['port'];
    }
    if (!empty($parsed['path'])) {
        $newuri .= $parsed['path'];
    }
    if (!empty($parsed['query'])) {
        $newuri .= '?' . $parsed['query'];
    }
    if (!empty($parsed['fragment'])) {
        $newuri .= '#' . $parsed['fragment'];
    }
    return $newuri;
}