|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
snojrnl Smarty n00b
Joined: 25 Nov 2003 Posts: 2
|
Posted: Thu Feb 05, 2004 9:50 pm Post subject: Stuck on an "auto-hyperlink" problem |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Feb 06, 2004 8:27 am Post subject: Re: Stuck on an "auto-hyperlink" problem |
|
|
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 |
|
|
|
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
|