Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Stuck on an "auto-hyperlink" problem

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
snojrnl
Smarty n00b


Joined: 25 Nov 2003
Posts: 2

PostPosted: Thu Feb 05, 2004 9:50 pm    Post subject: Stuck on an "auto-hyperlink" problem Reply with quote

Hi ... I'm trying to devise a modifier plugin that will "auto-hyperlink" any URLs in the given variable. On the surface it appears not hard to do, but I think there is more than meets the eye.

There are plenty of examples of regexps out there that will do this, but there are 2 major problems with them:

1. Every single example regexp I can find will completely destroy any existing A tags in the text.
2. Regexps were invented by satan and I have a hard time getting past anything simple.

Requirements...

1. The modifier must NOT screw up any URLs that are inside of any html tags, such as A or IMG.
2. The modifier should work with URLs specified as http://xxxx or www.xxxx.com/org/net/etc
3. Can this be done simply and efficiently without regexps that look like: ^l~"<>"}{(&*Olkaj*"<>"())>&"876.;ojlasy?<>"}{$@#^%@DFJSFU][ASDH}OArU}?{}??????

Anyone have anything?
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Feb 06, 2004 8:27 am    Post subject: Re: Stuck on an "auto-hyperlink" problem Reply with quote

snojrnl wrote:
3. Can this be done simply and efficiently without regexps that look like: ^l~"<>"}{(&*Olkaj*"<>"())>&"876.;ojlasy?<>"}{$@#^%@DFJSFU][ASDH}OArU}?{}??????


I think you have to decide. If it's simple it may not be efficient. If it's efficient (fast and optimized) it may not look simple anymore.

If the problem is too big divide it in smaller problems:
I'd use preg_split to seperate the <...>-tags from the content to be able to replace the links in the content more easily:

[php:1:819066a5b6]function smarty_modifier_url_replace($source) {
$parts = preg_split('!(<.*?>)!', $source, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
for ($i=0, $count=count($parts); $i<$count; $i++) {
if ($parts[$i]{0} != '<')
$parts[$i] =/* use a much simpler regexp here since you only replace content and don't have to keep <...>-tags in mind */
}
return implode('', $parts);
}[/php:1:819066a5b6]

[of course there are much more efficient solutions out there]
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP