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
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>';
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.