Jump to content

URL getting cut off by regex


reportingsjr

Recommended Posts

I have a regex that searches for urls in posts, and for some reason if the urls are longer than 35 characters it just cuts off the rest.

 

The regex:

\b((https?|ftp)://)?([a-z0-9](?:[-a-z0-9@]*[a-z0-9])?\.)+(com\b|edu\b|biz\b|gov\b|in(?:t|fo)\b|mil\b|net\b|org\b|[a-z][a-z]\b)(+)?(/[-a-z0-9_:@&?=+,.!/~*'\%\$]*)*(?<![.,?!])(?!((?!(?:<a )).)*?(?:</a>))

 

Thanks a ton!!

Link to comment
https://forums.phpfreaks.com/topic/77739-url-getting-cut-off-by-regex/
Share on other sites

No. I'll show you the full code:

(for reference, this is for a free php and js based web chat called Lace. (lacechat.com) so if you want to see all of the code just go there and download 1.6.1.)

<?php

$laceTextFilter = 'LaceLinkFilter';
$filterPriority = 100;

class LaceLinkFilter extends LaceTextFilter
{
var $urlRegex;
var $linkRegex;

function performTextFilter()
{
	$this->urlRegex  = "\b((https?|ftp)://)?([a-z0-9](?:[-a-z0-9@]*[a-z0-9])?\.)+(com\b|edu\b|biz\b|gov\b|in(?:t|fo)\b|mil\b|net\b|org\b|[a-z][a-z]\b)(+)?(/[-a-z0-9_:@&?=+,.!/~*'\%\$]*)*(?<![.,?!])(?!((?!(?:<a )).)*?(?:</a>))";
	$this->linkRegex = "\[(".$this->urlRegex.")\|(.*?)\]";

	$this->filterLinkSyntax();
	$this->filterBareUrls();

	return $this->text;
}

function filterLinkSyntax()
{
	$text = $this->text;
	$search = array();
	$replace = array();

	$matches = $this->getMatches("%".$this->linkRegex."%ix", $text);

	if ($matches)
	{
		$i = 0;
		foreach ($matches[0] as $match)
		{
			$url  = $matches[1][$i];
			$text = $matches[8][$i];

			$http = mb_substr($url, 0, 4);
			if ($http !== 'http' && $http !== 'ftp:')
				$url = 'http://'.$url;

			$search[] = $match;
			$replace[] = $this->makeLink($url, $text);

			$i++;
		}

		$text = str_replace($search, $replace, $this->text);
	}

	$this->text = $text;

}

function filterBareUrls()
{
	$text = $this->text;
	$search = array();
	$replace = array();

	$matches = $this->getMatches("%".$this->urlRegex."%ix", $text);

	if ($matches)
	{
		foreach ($matches[0] as $text)
		{
			$link = $text;
			$http = mb_substr($text, 0, 4);
			if ($http !== 'http' && $http !== 'ftp:')
				$link = 'http://'.$text;

			$search[] = $text;
			$replace[] = $this->makeLink($link, $text);
		}

		$text = str_replace($search, $replace, $this->text);
	}

	$this->text = $text;
}

function getMatches($regex, $text)
{
	$numMatches = preg_match_all($regex, $text, $matches);

	return ($numMatches > 0 ) ? $matches : false;
}

function makeLink($url, $text)
{
	return '<a href="'.$url.'" target="_blank" rel="external nofollow" class="external">'.$text.'</a>';
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.