src/UrlLinker.php
public function linkUrlsAndEscapeHtml($text)
	{
		// We can abort if there is no . in $text
		if ( strpos($text, '.') === false )
		{
			return $this->escapeHtml($text);
		}

		$html = '';

		$position = 0;

		$match = array();

		while (preg_match($this->buildRegex(), $text, $match, PREG_OFFSET_CAPTURE, $position))
		{
			list($url, $urlPosition) = $match[0];

			// Add the text leading up to the URL.
			$html .= $this->escapeHtml(substr($text, $position, $urlPosition - $position));

			$scheme      = $match[1][0];
			$username    = $match[2][0];
			$password    = $match[3][0];
			$domain      = $match[4][0];
			$afterDomain = $match[5][0]; // everything following the domain
			$port        = $match[6][0];
			$path        = $match[7][0];

			// Check that the TLD is valid or that $domain is an IP address.
			$tld = strtolower(strrchr($domain, '.'));

			$validTlds = $this->validTlds;

			if (preg_match('{^\.[0-9]{1,3}$}', $tld) || isset($validTlds[$tld]))
			{
				// Do not permit implicit scheme if a password is specified, as
				// this causes too many errors (e.g. "my email:foo@example.org").
				if ( ! $scheme && $password )
				{
					$html .= $this->escapeHtml($username);

					// Continue text parsing at the ':' following the "username".
					$position = $urlPosition + strlen($username);

					continue;
				}

				if ( ! $scheme && $username && ! $password && ! $afterDomain )
				{
					// Looks like an email address.
					$emailLinkCreator = $this->emailLinkCreator;

					// Add the hyperlink.
					$html .= $emailLinkCreator($url, $url);
				}
				else
				{
					// Prepend http:// if no scheme is specified
					$completeUrl = $scheme ? $url : "http://$url";
					$linkText = "$domain$port$path";

					$htmlLinkCreator = $this->htmlLinkCreator;

					// Add the hyperlink.
					$html .= $htmlLinkCreator($completeUrl, $linkText);
				}
			}
			else
			{
				// Not a valid URL.
				$html .= $this->escapeHtml($url);
			}

			// Continue text parsing from after the URL.
			$position = $urlPosition + strlen($url);
		}

		// Add the remainder of the text.
		$html .= $this->escapeHtml(substr($text, $position));

		return $html;
	}
public function linkUrlsAndEscapeHtml($text)
	{
		// We can abort if there is no . in $text
		if ( strpos($text, '.') === false )
		{
			return $this->escapeHtml($text);
		}

		$html = '';

		$position = 0;

		$match = array();

		while (preg_match($this->buildRegex(), $text, $match, PREG_OFFSET_CAPTURE, $position))
		{
			list($url, $urlPosition) = $match[0];

			// Add the text leading up to the URL.
			$html .= $this->escapeHtml(substr($text, $position, $urlPosition - $position));

			$scheme      = $match[1][0];
			$username    = $match[2][0];
			$password    = $match[3][0];
			$domain      = $match[4][0];
			$afterDomain = $match[5][0]; // everything following the domain
			$port        = $match[6][0];
			$path        = $match[7][0];

			// Check that the TLD is valid or that $domain is an IP address.
			$tld = strtolower(strrchr($domain, '.'));

			$validTlds = $this->validTlds;

			if (preg_match('{^\.[0-9]{1,3}$}', $tld) || isset($validTlds[$tld]))
			{
				// Do not permit implicit scheme if a password is specified, as
				// this causes too many errors (e.g. "my email:foo@example.org").
				if ( ! $scheme && $password )
				{
					$html .= $this->escapeHtml($username);

					// Continue text parsing at the ':' following the "username".
					$position = $urlPosition + strlen($username);

					continue;
				}

				if ( ! $scheme && $username && ! $password && ! $afterDomain )
				{
					// Looks like an email address.
					$emailLinkCreator = $this->emailLinkCreator;

					// Add the hyperlink.
					$html .= $emailLinkCreator($url, $url);
				}
				else
				{
					// Prepend http:// if no scheme is specified
					$completeUrl = $scheme ? $url : "http://$url";
					$linkText = "$domain$port$path";

					$htmlLinkCreator = $this->htmlLinkCreator;

					// Add the hyperlink.
					$html .= $htmlLinkCreator($completeUrl, $linkText);
				}
			}
			else
			{
				// Not a valid URL.
				$html .= $this->escapeHtml($url);
			}

			// Continue text parsing from after the URL.
			$position = $urlPosition + strlen($url);
		}

		// Add the remainder of the text.
		$html .= $this->escapeHtml(substr($text, $position));

		return $html;
	}
public function linkUrlsAndEscapeHtml ( $ text ) { // We can abort if there is no . in $text if ( strpos ( $ text , '.' ) === false ) { return $ this -> escapeHtml ( $ text ) ; } $ html = '' ; $ position = 0 ; $ match = array ( ) ; while ( preg_match ( $ this -> buildRegex ( ) , $ text , $ match , PREG_OFFSET_CAPTURE , $ position ) ) { list ( $ url , $ urlPosition ) = $ match [ 0 ] ; // Add the text leading up to the URL. $ html .= $ this -> escapeHtml ( substr ( $ text , $ position , $ urlPosition - $ position ) ) ; $ scheme = $ match [ 1 ] [ 0 ] ; $ username = $ match [ 2 ] [ 0 ] ; $ password = $ match [ 3 ] [ 0 ] ; $ domain = $ match [ 4 ] [ 0 ] ; $ afterDomain = $ match [ 5 ] [ 0 ] ; // everything following the domain $ port = $ match [ 6 ] [ 0 ] ; $ path = $ match [ 7 ] [ 0 ] ; // Check that the TLD is valid or that $domain is an IP address. $ tld = strtolower ( strrchr ( $ domain , '.' ) ) ; $ validTlds = $ this -> validTlds ; if ( preg_match ( '{^\.[0-9]{1,3}$}' , $ tld ) || isset ( $ validTlds [ $ tld ] ) ) { // Do not permit implicit scheme if a password is specified, as // this causes too many errors (e.g. "my email:foo@example.org"). if ( ! $ scheme && $ password ) { $ html .= $ this -> escapeHtml ( $ username ) ; // Continue text parsing at the ':' following the "username". $ position = $ urlPosition + strlen ( $ username ) ; continue ; } if ( ! $ scheme && $ username && ! $ password && ! $ afterDomain ) { // Looks like an email address. $ emailLinkCreator = $ this -> emailLinkCreator ; // Add the hyperlink. $ html .= $ emailLinkCreator ( $ url , $ url ) ; } else { // Prepend http:// if no scheme is specified $ completeUrl = $ scheme ? $ url : "http://$url" ; $ linkText = "$domain$port$path" ; $ htmlLinkCreator = $ this -> htmlLinkCreator ; // Add the hyperlink. $ html .= $ htmlLinkCreator ( $ completeUrl , $ linkText ) ; } } else { // Not a valid URL. $ html .= $ this -> escapeHtml ( $ url ) ; } // Continue text parsing from after the URL. $ position = $ urlPosition + strlen ( $ url ) ; } // Add the remainder of the text. $ html .= $ this -> escapeHtml ( substr ( $ text , $ position ) ) ; return $ html ; }
Transforms plain text into valid HTML, escaping special characters and
turning URLs into links.

@param string $text
@return string
Transforms plain text into valid HTML escaping special characters and turning URLs into links .