URL encoder according to RFC 3986
Returns a string containing the encoded URL with disallowed characters converted to their percentage encodings.

@link http://publicmind.in/blog/url-encoding/

@param  string $url
@return string string
556233
protected static function encode_url($url)
    {
        $reserved = array(
            ':' => '!%3A!ui',
            '/' => '!%2F!ui',
            '?' => '!%3F!ui',
            '#' => '!%23!ui',
            '[' => '!%5B!ui',
            ']' => '!%5D!ui',
            '@' => '!%40!ui',
            '!' => '!%21!ui',
            '$' => '!%24!ui',
            '&' => '!%26!ui',
            "'" => '!%27!ui',
            '(' => '!%28!ui',
            ')' => '!%29!ui',
            '*' => '!%2A!ui',
            '+' => '!%2B!ui',
            ',' => '!%2C!ui',
            ';' => '!%3B!ui',
            '=' => '!%3D!ui',
            '%' => '!%25!ui'
        );
        $url = preg_replace(array_values($reserved), array_keys($reserved), rawurlencode($url));
        return $url;
    }
712364
private static function cleanUrl($url)
    {
        $result = '';
        $inner = false;

        for ($i = 0; $i < strlen($url); $i++) {
            if (!$inner) {
                if ($url[$i] != '{') {
                    $result .= $url[$i];
                } else {
                    $result .= '{';
                    $inner = true;
                }
            } else {
                if ($url[$i] == '}') {
                    $result .= '}';
                    $inner = false;
                }
            }
        }

        return $result;
    }
1091628
public function prepUrl($url)
	{
        if (empty($url)) {
            return '';
        }

        return parse_url($url, PHP_URL_SCHEME) === null ? 'http://' . $url : $url;
	}
88414
public static function wwwLess($url) {
		if (strpos($url, 'www.') === 0 && substr_count($url, '.') > 1) {
			$url = substr($url, 4);
		}
		return $url;
	}
881602
public function encodeValue($value)
    {
        if ($this->urlEncode == self::RFC_3986) {
            return rawurlencode($value);
        } elseif ($this->urlEncode == self::FORM_URLENCODED) {
            return urlencode($value);
        } else {
            return (string) $value;
        }
    }
1066746
public static function urlToPath($url)
    {
        $path = str_replace('http://', '', $url);
        $path = str_replace('https://', '', $path);
        $path = str_replace($_SERVER['HTTP_HOST'], '', $path);

        if (substr($path, 0, 1) == '/') {
            $path = substr($path, 1);
        }

        return $_SERVER['DOCUMENT_ROOT'] . '/' . $path;
    }
596248
public static function codecURL( $url, $encode )
    {
        $originalLocale = setlocale( LC_CTYPE, "0" );
        setlocale( LC_CTYPE, 'C' );
        // Parse URL and encode the path.
        $data = parse_url( $url );
        setlocale( LC_CTYPE, $originalLocale );

        if ( isset( $data['path'] ) )
        {
            if ( $encode )
                $data['path'] = eZURI::encodeIRI( $data['path'] ); // Make sure it is encoded to IRI format
            else
                $data['path'] = eZURI::decodeIRI( $data['path'] ); // Make sure it is dencoded to internal encoding
        }

        // Reconstruct the URL
        $host    = '';
        $preHost = '';
        if ( isset( $data['user'] ) )
        {
            if ( isset( $data['pass'] ) )
                $preHost .= $data['user'] . ':' . $data['pass'] . '@';
            else
                $preHost .= $data['user'] . '@';
        }
        if ( isset( $data['host'] ) )
        {
            if ( isset( $data['port'] ) )
                $host = $preHost . $data['host'] . ':' . $data['port'];
            else
                $host = $preHost . $data['host'];
        }
        $url = '';
        if ( isset( $data['scheme'] ) )
            $url = $data['scheme'] . '://' . $host;
        else if ( strlen( $host ) > 0 )
            $url = '//' . $host;
        if ( isset( $data['path'] ) )
        {
            $url .= $data['path'];
        }
        if ( isset( $data['query'] ) )
        {
            $url .= '?' . $data['query'];
        }
        if ( isset( $data['fragment'] ) )
        {
            $url .= '#' . $data['fragment'];
        }

        return $url;
    }
407020
public function queryString($url = null)
    {
        $query = '';

        if (!$url)
        {
            $url = $this->server('request_uri');
        }

        $position = mb_strpos($url, '?');

        if ($position !== false)
        {
            $query = mb_substr($url, $position + 1);
        }

        return $query;
    }
540478
protected function getUrlContents(string $url): string
    {
        $request = $this->getRequest($url);

        return $this->getParsedResponse($request);
    }
502252
protected function parseUrl($url = "")
    {
        $parsed = ($url) ? parse_url($url) : parse_url($this->_env['REQUEST_URI']);
        
        if (preg_match('/[a-zA-Z0-9_]+\.php/i', $parsed['path'], $matches)) {
            $parsed['path'] = preg_replace("/$matches[0]/", '/', $parsed['path']);
        }
        
        return $parsed;
    }
95394
public static function trimUrl($url) {
		$result = str_replace ( "//", "/", $url );
		while ( $result !== $url ) {
			$url = $result;
			$result = str_replace ( "//", "/", $url );
		}
		return $result;
	}
940300
protected function formatUrl(string $url): string
    {
        return Str::startsWith($url, '/')
            ? $this->app['url']->to($url)
            : $url;
    }
1241978
public function normalizeLinkText($url)
    {
        if( property_exists($this,'normalizeLinkText') && is_callable($this->normalizeLinkText)){
            $fn = $this->normalizeLinkText;
            return $fn($url);
        }
        $parsed = $this->mdurl->parse($url, true);

        if ($parsed->hostname) {
            // Encode hostnames in urls like:
            // `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
            //
            // We don't encode unknown schemas, because it's likely that we encode
            // something we shouldn't (e.g. `skype:name` treated as `skype:host`)
            //
            $is = false;
            foreach (self::RECODE_HOSTNAME_FOR as &$v) {
                if( ($is = $v === $parsed->protocol) ) break;
            }
            if (empty($parsed->protocol) || $is) {
                try {
                    $parsed->hostname = $this->punycode->toUnicode($parsed->hostname);
                } catch (\Exception $e) { /**/ }
            }
        }
        return $this->mdurl->decode($this->mdurl->format($parsed));
    }
916023
protected function mbParseUrl($url)
  {
    $enc_url = preg_replace_callback(
      '%[^:/@?&=#]+%usD',
      function ($matches) {
        return urlencode($matches[0]);
      },
      $url
    );

    if ($parts = parse_url($enc_url)) {
      foreach ($parts as $name => $value) {
        $parts[$name] = urldecode($value);
      }
    }

    return $parts;
  }
354041
final static function normalize(string $url):string{#{{{

    if(isset(
      $_SERVER['REQUEST_SCHEME'],
      $_SERVER['REQUEST_HOST'],
      $_SERVER['REQUEST_PORT'],
      $_SERVER['REQUEST_URI'],
      $_SERVER['SERVER_PORT']
    ) || PHP_SAPI ==='cli')
      return $url;

    $arr = parse_url($url);
    if($arr===false) return '';

    $a = [
      $arr['scheme']??$_SERVER['REQUEST_SCHEME'],
      '://',
      $arr['user']??'',
      ':',
      $arr['pass']??'',
      '@',
      $arr['host']??$_SERVER['HTTP_HOST'],
      ':',
      $arr['port']??$_SERVER['SERVER_PORT'],
      substr($_SERVER['REQUEST_URI'],0, strrpos($_SERVER['REQUEST_URI'],'/')+1),
      $arr['path']??'',
      '?',
      $arr['query']??'',
      '#',
      $arr['fragment']??'',
    ];

    if(!in_array($a[0], ['http','https'])) return '';

    if($a[12]==='')            unset($a[11],$a[12]);
    if($a[14]==='')            unset($a[13],$a[14]);
    if($a[8]==80)              unset($a[7],$a[8]);
    if($a[4]==='')             unset($a[3],$a[4]);
    if($a[2]==='')             unset($a[2],$a[3],$a[4],$a[5]);
    if(strpos($a[10],'/')===0) unset($a[9]);

    while(strpos($a[10],'../')===0){
      $a[10] = substr($a[10],3);
      $a[9] = dirname($a[9]);
      if($a[9]!=='/') $a[9] .= '/';
    }

    while(strpos($a[10],'./')===0){
      $a[10] = substr($a[10],2);
    }

    if($a[10]==='.') unset($a[10]);

    return implode($a);
  }
351278
private function getUrl($url)
	{
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_HEADER,0);
		// curl_setopt($ch, CURLOPT_ENCODING, "");
		$output = curl_exec($ch);
		curl_close($ch);
	    return $output;
	}
62700
public static function normalizeURL($url)
  {
    $url_parts = self::splitURL($url);
    
    if ($url_parts == null) return null;
    
    $url_normalized = self::buildURLFromParts($url_parts, true);
    return $url_normalized;
  }