/**
* Transforms a UTF-8 URL to a Punycode URL
*
* @param string $uri The UTF-8 URL to transform
*
* @return string The punycode URL
*
* @since 3.1.2
*/
public static function urlToPunycode($uri)
{
$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 = static::toPunycode($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;
}