PHP Code:
function hyperlink($message)
{
// If the message contains the string "<a" at all, then don't process it:
if (eregi("<a", $message))
{
return ($message);
}
$tagster = new Tagster("_new","",60,FALSE);
$message = $tagster->transform($message);
return ($message);
}
/**
* File : tagster_lib.php
* Version : 3.0
* Date : September 22nd, 2002
* Author : Lars B. Jensen, lars.jensen@ljweb.com
*
* Module Description
* Module to transform URL and E-Mail addresses into clickable links.
*
* Instruction
* To utilize the tagster_lib library, copy this file to a location on your server. Include the
* library into your php document or template.
* Use the function $tagster->transform to extract and modify all links and emails in a given string.
* The parameters $target handling the target for links and $class for handling css on <a tags are
* both optional.
*
* Example
* include('lib/tagster_lib.php');
* $tagster = new Tagster("_blank", "mystyle");
* $str = $tagster->transform($str);
*
* Note
* This module library has support for the scandinavian specialchars æ, ø and å.
*
*
* Public Functions
* --------------------------------------------------------------´
* function Tagster::format($str)
* function Tagster::url($str, $target, $class)
* function Tagster::email($str, $class)
*
* Private Functions
* --------------------------------------------------------------´
* function Tagster::fix_endchar($str)
* function Tagster::expand($str)
* function Tagster::reduce($str)
* function Tagster::fix_longword($str, $maxlen)
*/
class Tagster
{
var $target;
var $css_class;
var $fixlongword;
var $replace_entities;
function Tagster($target="_blank", $css_class="", $fixlongword = 0, $replace_entities = TRUE) {
$this->target = $target;
$this->css_class = $css_class;
$this->fixlongword = $fixlongword;
$this->replace_entities = $replace_entities;
}
function transform($str) {
if ($this->replace_entities) {
$str = str_replace("&", "&", $str);
$str = str_replace("<", "<", $str);
$str = str_replace(">", ">", $str);
}
$str = $this->url($str, $this->target, $this->css_class);
$str = $this->email($str, $this->css_class);
$str = $this->fix_endchar($str);
if ($this->replace_entities) {
$str = str_replace(" ", " ", $str);
$str = str_replace("\t", " ", $str);
$str = str_replace("\r", "", $str);
$str = str_replace("\n", "<br />", $str);
}
if ($this->fixlongword != 0) {
$str = $this->fix_longword($str, $this->fixlongword);
}
return $str;
}
function url($str, $target, $css_class) {
$ins_str = "";
if ($css_class) $ins_str .= " class=\"".$css_class."\"";
if ($target) $ins_str .= " target=\"".$target."\"";
$str = $this->expand($str);
$str = preg_replace ("/(ftp|http|https|telnet|news|nntp|file|irc):\/\/([a-z0-9~#%@&:;=!',_æøå\(\)\?\/\.\-\+\[\]\|\*\$\^\{\}]+)/i", "<b><a href=\"\\1://\\2\"".$ins_str." title=\"External link.\">LINK</a></b>", $str);
$str = preg_replace ("/(\s|tp\:|\(|\[|\>)(www\.)([a-z0-9~#%@&:;=!',_æøå\(\)\?\/\.\-\+\[\]\|\*\$\^\{\}]+)/i", "\\1<b><a href=\"http://\\2\\3\"".$ins_str." title=\"External link.\">LINK</a></b>", $str);
$str = preg_replace ("/(\s|tp\:|\(|\[|\>)(ftp\.)([a-z0-9~#%@&:;=!',_æøå\(\)\?\/\.\-\+\[\]\|\*\$\^\{\}]+)/i", "\\1<b><a href=\"ftp://\\2\\3\"".$ins_str." title=\"External link.\">LINK</a></b>", $str);
return $this->reduce($str);
}
function email($str, $css_class="") {
$ins_class = "";
if ($css_class) $ins_class = " class=\"".$css_class."\"";
$str = $this->expand($str);
$str = preg_replace ("/([\s\"])([\w\.\-_]+)@([\w\-_]+)\.([\w\.\-_]+)/i", "\\1<a href=\"mailto:\\2@\\3.\\4\"".$ins_class.">\\2@\\3.\\4</a>", $str);
return $this->reduce($str);
}
function fix_endchar($str) {
$str = preg_replace ("/([\'\"\)\]\.\,\?\!]+)\">/i", "\">", $str);
$str = preg_replace ("/([\'\"\)\]\.\,\?\!]+)\" (target|class)=\"/i", "\" \\2=\"", $str);
$str = preg_replace ("/([\'\"\)\]\.\,\?\!]+)<\/a>/i", "</a>\\1", $str);
return $str;
}
function fix_longword($str, $maxlen = 10) {
$sw = 0;
$in_tag = false;
$str_size = strlen($str);
$res = "";
for ($i=0; $i<$str_size; $i++){
if ($str[$i] == '<') $in_tag = true;
else if ($str[$i] == ' ') $sw = 0;
if (!$in_tag && (($sw++) > $maxlen)) {
$res .= '­';
$sw = 0;
}
if ($str[$i] == '>') $in_tag = false;
$res .= $str[$i];
}
return $res;
}
function expand($str) {
return " ".$str." ";
}
function reduce($str) {
return substr($str, 1, -1);
}
}
Bookmarks