|
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 |
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Nov 09, 2005 8:02 pm Post subject: modifier: addlinks |
|
|
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 |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Thu Nov 10, 2005 4:07 pm Post subject: Re: modifier: addlinks |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Nov 10, 2005 4:14 pm Post subject: Re: modifier: addlinks |
|
|
kills wrote: | why do you replace '&' with '&' ? |
I think the crappy phpBB slurps the & . |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Nov 10, 2005 7:56 pm Post subject: Re: modifier: addlinks |
|
|
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 |
|
optimistic Smarty n00b
Joined: 20 Aug 2006 Posts: 2 Location: Atlanta, GA
|
Posted: Sun Aug 20, 2006 8:47 pm Post subject: Where does this code go |
|
|
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 |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
|
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
|