Outputs a "safe" url, stripped of xss, autolink on domain, and stripped of invalid chars

@param string $url The url to process

@return string Safe and happy URL
193955
public static function safeUrl($url)
    {
        // Strip invalid characters (anything but [a-zA-Z0-9=?&:;#_\/.-])
//        $url = preg_replace("/[^a-zA-Z0-9=?&:;#_\/.-]/", '', $url);

        if (empty($url))
            return '';

        // Prepend http:// if no scheme is specified
        if (!preg_match("/^(https?):\/\//", $url))
            $url = 'http://' . $url;

        if (!self::isURL($url))
            $url = '';

        return filter_var($url,  FILTER_SANITIZE_URL);

    }
1343925
public static function extractFromURL($url)
		{
			$html = file_get_contents($url);
			
			if($html === FALSE) 
			{
				return;
			}
			
			return self::extractFromHTML($html, $url);
		}
252177
public static function httpLess($url) {
		if (stripos($url, 'http://') === 0) {
			$url = substr($url, 7);
		} elseif (stripos($url, 'https://') === 0) {
			$url = substr($url, 8);
		}
		return $url;
	}
598117
public function urlencode(string $url): string
    {
        $replacements = [
            '%' => '%25',
            '&' => '%26',
            '<' => '%3C',
            '>' => '%3E',
            '|' => '%7C',
        ];

        return str_replace(array_keys($replacements), array_values($replacements), $url);
    }
1193931
private function buildUrl($url)
    {
        $url = $this->protocol . '://' . $url . '/';

        if ($this->date) {
            $url .= $this->date;
        } else {
            $url .= 'latest';
        }

        $url .= '?base=' . $this->base;

        if ($this->key) {
            $url .= '&access_key=' . $this->key;
        }

        if ($symbols = $this->symbols) {
            $url .= '&symbols=' . implode(',', $symbols);
        }

        return $url;
    }
844998
public function parseURL( $url ) {
		// Feeding fully encoded URLs will not work.  So let's detect and decode if needed first.
		// This is just idiot proofing.
		// See if the URL is fully encoded by checking if the :// is encoded.
		// This prevents URLs where double encoded values aren't mistakenly decoded breaking the URL.
		if ( preg_match( '/^([a-z0-9\+\-\.]*)(?:%3A%2F%2F|%3A\/\/|:%2F%2F)/i', $url ) ) {
			// First let's break the fragment out to prevent accidentally mistaking a decoded %23 as a #
			$fragment = parse_url( $url, PHP_URL_FRAGMENT );
			if ( !is_null( $fragment ) ) {
				$url = strstr( $url, "#", true );
			}
			// Decode URL
			$url = rawurldecode( $url );
			// Re-encode the remaining #'s
			$url = str_replace( "#", "%23", $url );
			// Reattach the fragment
			if ( !is_null( $fragment ) ) {
				$url .= "#$fragment";
			}
		}
		// Sometimes the scheme is followed by a single slash instead of a double.
		// Web browsers and archives support this, so we should too.
		if ( preg_match( '/^([a-z0-9\+\-\.]*:)?\/([^\/].+)/i', $url, $match ) ) {
			$url = $match[1] . "//" . $match[2];
		}
		// Sometimes protocol relative URLs are not formatted correctly
		// This checks to see if the URL starts with :/ or ://
		// We will assume http in these cases
		if ( preg_match( '/^:\/\/?([^\/].+)/i', $url, $match ) ) {
			$url = "http://" . $match[1];
		}
		// If we're missing the scheme and double slashes entirely, assume http.
		// The parse_url function fails without this
		if ( !preg_match( '/(?:[a-z0-9\+\-\.]*:)?\/\//i', $url ) ) {
			$url = "http://" . $url;
		}
		$encodedUrl = preg_replace_callback(
			'%[^:/@?&=#;]+%sD',
			function ( $matches ) {
				return urlencode( $matches[0] );
			},
			$url
		);
		$parts = parse_url( $encodedUrl );
		// Check if the URL was actually parsed.
		if ( $parts !== false ) {
			foreach ( $parts as $name => $value ) {
				$parts[$name] = urldecode( $value );
			}
		}

		return $parts;
	}
898229
public static function validate_url(String $url)
    {
        // https://mathiasbynens.be/demo/url-regex
        // @diegoperini (502 chars)
        $regex = '_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.1';
        $regex .= '68(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d';
        $regex .= '|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?';
        $regex .= ':[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS';

        if (preg_match($regex, $url))
            return true;

        return false;
    }
286853
public static function getUrlWithProtocol($url) {
		if(empty($url)) {
			return $url;
		}
		list($scheme, $host) = self::splitUrl($url);
		if(empty($scheme) || empty($host)) {
			return '';
		}
		return $scheme.'://'.$host;
	}
166196
public static function prepareUrl($url)
    {
        // $url = urlencode($url);

        $replacement = [
            '&' => '&amp;',
            "'" => '&apos;',
            '"' => '&quot;',
            '>' => '&gt;',
            '<' => '&lt;',
        ];

        $url = str_replace(array_keys($replacement), array_values($replacement), $url);

        return $url;
    }
512712
public function convertUrl(string $url): string
    {
        $parsed = parse_url($url);
        if ($parsed === false) {
            throw new InvalidArgumentException('The given string does not look like a URL', 206);
        }

        // Nothing to do
        if ($parsed['host'] === null) {
            return $url;
        }
        $parsed['host'] = $this->convert($parsed['host']);

        return http_build_url($parsed);
    }
938301
public static function extractUrl($url)
    {
        if (is_string($url)) {
            // Extract the URL from a given CSS URL value|string
            //
            // Examples:
            // - "http://www.example.com"
            // - 'http://www.example.com'
            // - url("http://www.example.com")
            // - url( 'http://www.example.com' )
            // - url(http://www.example.com)
            // - url( http://www.example.com )
            // - url("http://www.example.com?escape=\"escape\"")
            //
            // "Parentheses, whitespace characters, single quotes (') and double quotes (") appearing in a URL must be
            // escaped with a backslash so that the resulting value is a valid URL token"
            $url = Placeholder::replaceStringPlaceholders($url);
            $url = preg_replace('/^url\(|\)$/', '', $url);
            $url = str_replace(["\\(", "\\)", "\\ ", "\\'", "\\\""], ["(", ")", " ", "'", "\""], $url);
            $url = preg_replace('/^(["\'])(.*)\g{1}$/', '\\2', $url);
            $url = urldecode($url);

            return $url;
        } else {
            throw new \InvalidArgumentException(
                "Invalid type '" . gettype($url). "' for argument 'url' given."
            );
        }
    }
919349
protected static function parseUrl($url)
	{
		$regexp = '(^(?:([a-z][-+.\\w]*):)?(?://(?:([^:/?#]*)(?::([^/?#]*)?)?@)?(?:(\\[[a-f\\d:]+\\]|[^:/?#]+)(?::(\\d*))?)?(?![^/?#]))?([^?#]*)(\\?[^#]*)?(#.*)?$)Di';

		// NOTE: this regexp always matches because of the last three captures
		preg_match($regexp, $url, $m);

		$parts  = [];
		$tokens = ['scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment'];
		foreach ($tokens as $i => $name)
		{
			$parts[$name] = (isset($m[$i + 1])) ? $m[$i + 1] : '';
		}

		/**
		* @link http://tools.ietf.org/html/rfc3986#section-3.1
		*
		* 'An implementation should accept uppercase letters as equivalent to lowercase in
		* scheme names (e.g., allow "HTTP" as well as "http") for the sake of robustness but
		* should only produce lowercase scheme names for consistency.'
		*/
		$parts['scheme'] = strtolower($parts['scheme']);

		/**
		* Normalize the domain label separators and remove trailing dots
		* @link http://url.spec.whatwg.org/#domain-label-separators
		*/
		$parts['host'] = rtrim(preg_replace("/\xE3\x80\x82|\xEF(?:\xBC\x8E|\xBD\xA1)/s", '.', $parts['host']), '.');

		// Test whether host has non-ASCII characters and punycode it if possible
		if (preg_match('#[^[:ascii:]]#', $parts['host']) && function_exists('idn_to_ascii'))
		{
			$variant = (defined('INTL_IDNA_VARIANT_UTS46')) ? INTL_IDNA_VARIANT_UTS46 : 0;
			$parts['host'] = idn_to_ascii($parts['host'], 0, $variant);
		}

		return $parts;
	}
1150909
private function processUrl($url)
    {
        $vars = [
            '{CLIENT_ID}'    => $this->getClientId(),
            '{REDIRECT_URI}' => $this->getRedirectURI(),
            '{SCOPE}'        => $this->getScope(),
            '{STATE}'        => $this->getState(),
            " "              => '',
            "\n"             => '',
            "\r"             => '',
            "\t"             => ''
        ];

        $url = str_replace(array_keys($vars), array_values($vars), $url);

        return $url;
    }
896898
private function transformSshUrlIntoHttp($url)
    {
        $pattern = '#git@' . $this->getDomain() . ':' . self::REGEX_USER . '/' . self::REGEX_REPOSITORY . '.git$#';

        if (preg_match($pattern, $url, $matches)) {
            return sprintf(
                'https://%s/%s/%s',
                $this->getDomain(),
                $matches['user'],
                $matches['repository']
            );
        }

        return $url;
    }
754696
public static function parseUrl($url)
    {
        if (!\is_string($url)) {
            throw new \InvalidArgumentException('URL must be a string');
        }

        $encodedUrl = preg_replace_callback(
            '%[^:/@?&=#]+%u',
            function ($matches) { return rawurlencode($matches[0]); },
            $url
        );

        $parts = \is_string($encodedUrl) ? parse_url($encodedUrl) : false;
        if ($parts === false) {
            throw new \InvalidArgumentException("Malformed URL: {$url}");
        }

        return $parts;
    }
241440
public function absolutizeUrl($url)
    {
        if (!$this->_baseUrl || preg_match('#^https?://#i', $url) || strpos($url, 'javascript:') === 0) {
            return $url;
        }

        if ($url === '') {
            return $this->_baseUrl;
        } elseif ($url[0] === '/') {
            return substr($this->_baseUrl, 0, strpos($this->_baseUrl, '/', 10)) . $url;
        } elseif ($url[0] === '#') {
            if (($pos = strrpos($this->_sourceUrl, '#')) === false) {
                return $this->_sourceUrl . $url;
            } else {
                return substr($this->_sourceUrl, 0, $pos) . $url;
            }
        } else {
            return substr($this->_baseUrl, 0, strrpos($this->_baseUrl, '/', 10) + 1) . $url;
        }
    }
1058008
protected function prepareUrl($url)
    {
        $replacement = ($this->removeHostFromUrl ? '' : '$1') . ($this->removeScriptFromUrl ? '' : '$2');

        return preg_replace('#(https?\://[^/]+)(/[^/\.]+\.php)?#', $replacement, $url);
    }