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

modifier: addlinks

 
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
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Nov 09, 2005 8:02 pm    Post subject: modifier: addlinks Reply with quote

This modifier will automatically hyperlink URLs in text, and also hyperlink email addresses, javascript encoded. It is smart enough to handle http:// addresses, as well as www. addresses without the http:// prefix. It also truncates the url text in the link.

[php:1:e93d20b655]<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/


/**
* Smarty addlinks modifier plugin
*
* Type: modifier<br>
* Name: addlinks<br>
* Date: Feb 24, 2003
* Purpose: replace html links with html tags
* Input: string of text with tags
* Example: {$var|addlinks}
* @author Monte Ohrt <monte@ohrt.com>
* @version 1.0
* @param string
* @param string
* @return string
*/
function smarty_modifier_addlinks($string)
{

$string = preg_replace_callback('!((?:https?://|www\.)[^/\s]*/?)(\S*)!','urladdr_callback',$string);
$string = preg_replace_callback('!\S+@\S+!','emailaddr_callback',$string);
return $string;

}

function urladdr_callback($matches) {
if(preg_match('!^https?://!',$matches[0]))
$link = $matches[0];
else
$link = 'http://' . $matches[0];
$ext = !empty($matches[2]) ? '...' . substr($matches[2],-6) : '';
return '<a href="' . str_replace('&','&',$link) . '" target="_blank" rel="nofollow">' . $matches[1] . $ext . '</a>';
}

function emailaddr_callback($matches) {
$string = '<a href="mailto:' . $matches[0] . '">' . $matches[0] . '</a>';

for($x = 0, $y = strlen($string); $x < $y; $x++ ) {
$ord[] = ord($string[$x]);
}

$_ret = "<script type=\"text/javascript\" language=\"javascript\">";
$_ret .= "{document.write(String.fromCharCode(";
$_ret .= implode(',',$ord);
$_ret .= "))";
$_ret .= "}";
$_ret .= "</script>";

return $_ret;
}

/* vim: set expandtab: */

?>[/php:1:e93d20b655]


Last edited by mohrt on Thu Nov 10, 2005 7:56 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Thu Nov 10, 2005 4:07 pm    Post subject: Re: modifier: addlinks Reply with quote

Hi monte,

[php:1:b78e401212]return '<a href="' . str_replace('&','&',$link) . '" target="_blank" rel="nofollow">'[/php:1:b78e401212]

why do you replace '&' with '&' ?

Bye,
Markus
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Thu Nov 10, 2005 4:14 pm    Post subject: Re: modifier: addlinks Reply with quote

kills wrote:
why do you replace '&' with '&' ?


I think the crappy phpBB slurps the &amp; .
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Nov 10, 2005 7:56 pm    Post subject: Re: modifier: addlinks Reply with quote

kills wrote:
Hi monte,

[php:1:57617526ee]return '<a href="' . str_replace('&','&',$link) . '" target="_blank" rel="nofollow">'[/php:1:57617526ee]

why do you replace '&' with '&' ?

Bye,
Markus


It's fixed. thanks.
Back to top
View user's profile Send private message Visit poster's website
optimistic
Smarty n00b


Joined: 20 Aug 2006
Posts: 2
Location: Atlanta, GA

PostPosted: Sun Aug 20, 2006 8:47 pm    Post subject: Where does this code go Reply with quote

Hi,
I read your post and would like to know where this goes. Do I create a new directory or what. Please advise.

Thanks
_________________
Determination Determines Destiny!
Back to top
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Thu Aug 31, 2006 2:08 pm    Post subject: Reply with quote

http://smarty.php.net/manual/en/plugins.php
Back to top
View user's profile Send private message
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