Jeg bruger disse funktioner:
function text_format($str, $target="_blank", $css_class="", $fixlongword = 0)
{
$str = str_replace("&", "&", $str);
$str = str_replace("<", "<", $str);
$str = str_replace(">", ">", $str);
$str = text_url($str, $target, $css_class);
$str = text_email($str, $css_class);
$str = text_fix_endchar($str);
$str = str_replace(" ", " ", $str);
$str = str_replace("\t", " ", $str);
$str = str_replace("\r", "", $str);
$str = str_replace("\n", "<br>", $str);
if ($fixlongword != 0)
$str = text_fix_longword($str, $fixlongword);
$str = addslashes($str);
return $str;
}
function text_url($str, $target, $css_class)
{
$ins_str = "";
if ($css_class)
$ins_str .= " class=\"".$css_class."\"";
if ($target)
$ins_str .= " target=\"".$target."\"";
$str = preg_replace_callback ("/(ftp|http|https|telnet|news|nntp|file|irc):\/\/([a-z0-9~#&:;=!',_æøå\(\)\?\/\.\-\+\[\]\|\*\$\%\^\{\}]+)/i", create_function('$matches','if( strlen($matches[2]) > 25 ) $name = substr($matches[2],0,25) . ".."; else $name = $matches[2]; return "<a".$ins_str." href=\"{$matches[1]}://{$matches[2]}\">$name</a>";'),$str);
$str = preg_replace_callback ("/(\s|tp\:|\(|\[|\>)(www\.)([a-z0-9~#&:;=!',_æøå\(\)\?\/\.\-\+\[\]\|\*\$\%\^\{\}]+)/i", create_function('$matches','if( strlen($matches[3]) > 25 ) $name = substr($matches[3],0,25) . ".."; else $name = $matches[3]; return "<a".$ins_str." href=\"
http://$matches[2]$matches[3]\">$name</a>";'), $str);
$str = preg_replace_callback ("/(\s|tp\:|\(|\[|\>)(ftp\.)([a-z0-9~#&:;=!',_æøå\(\)\?\/\.\-\+\[\]\|\*\$\%\^\{\}]+)/i", create_function('$matches','if( strlen($matches[3]) > 25 ) $name = substr($matches[3],0,25) . ".."; else $name = $matches[3]; return "<a".$ins_str." href=\"
ftp://$matches[2]$matches[3]\">$name</a>";'), $str);
return $str;
}
function text_email($str, $css_class="")
{
$ins_class = "";
if ($css_class)
$ins_class = " class=\"".$css_class."\"";
$str = text_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 text_reduce($str);
}
function text_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 text_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 text_expand($str)
{
return " ".$str." ";
}
function text_reduce($str)
{
return substr($str, 1, -1);
}