Contao\Idna::encodeUrl PHP Method

encodeUrl() public static method

Encode the domain in an URL
public static encodeUrl ( string $strUrl ) : string
$strUrl string The URL
return string The encoded URL
    public static function encodeUrl($strUrl)
    {
        if ($strUrl == '') {
            return '';
        }
        // Empty anchor (see #3555)
        if ($strUrl == '#') {
            return $strUrl;
        }
        // E-mail address
        if (strncasecmp($strUrl, 'mailto:', 7) === 0) {
            return static::encodeEmail($strUrl);
        }
        $arrUrl = parse_url($strUrl);
        // Add the scheme to ensure that parse_url works correctly
        if (!isset($arrUrl['scheme']) && strncmp($strUrl, '{{', 2) !== 0) {
            $arrUrl = parse_url('http://' . $strUrl);
            unset($arrUrl['scheme']);
        }
        // Scheme
        if (isset($arrUrl['scheme'])) {
            $arrUrl['scheme'] .= substr($strUrl, strlen($arrUrl['scheme']), 3) == '://' ? '://' : ':';
        }
        // User
        if (isset($arrUrl['user'])) {
            $arrUrl['user'] .= isset($arrUrl['pass']) ? ':' : '@';
        }
        // Password
        if (isset($arrUrl['pass'])) {
            $arrUrl['pass'] .= '@';
        }
        // Host
        if (isset($arrUrl['host'])) {
            $arrUrl['host'] = static::encode($arrUrl['host']);
        }
        // Port
        if (isset($arrUrl['port'])) {
            $arrUrl['port'] = ':' . $arrUrl['port'];
        }
        // Path does not have to be altered
        // Query
        if (isset($arrUrl['query'])) {
            $arrUrl['query'] = '?' . $arrUrl['query'];
        }
        // Anchor
        if (isset($arrUrl['fragment'])) {
            $arrUrl['fragment'] = '#' . $arrUrl['fragment'];
        }
        return $arrUrl['scheme'] . $arrUrl['user'] . $arrUrl['pass'] . $arrUrl['host'] . $arrUrl['port'] . $arrUrl['path'] . $arrUrl['query'] . $arrUrl['fragment'];
    }

Usage Example

Esempio n. 1
0
 /**
  * Encode the domain in an URL
  *
  * @param string $strUrl The URL
  *
  * @return string The encoded URL
  *
  * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  *             Use Idna::encodeUrl() instead.
  */
 protected function idnaEncodeUrl($strUrl)
 {
     @trigger_error('Using System::idnaEncodeUrl() has been deprecated and will no longer work in Contao 5.0. Use Idna::encodeUrl() instead.', E_USER_DEPRECATED);
     return \Idna::encodeUrl($strUrl);
 }