reportingsjr Posted November 17, 2007 Share Posted November 17, 2007 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!! Quote Link to comment Share on other sites More sharing options...
toplay Posted November 17, 2007 Share Posted November 17, 2007 I don't see any limitation in that expression. You might want to look at your code or if you're saving it in a database, I bet you've limited the column size to 35 characters. Quote Link to comment Share on other sites More sharing options...
reportingsjr Posted November 18, 2007 Author Share Posted November 18, 2007 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>'; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.