function c2c_hyperlink_urls ($text, $mode='0', $trunc_before='', $trunc_after='...', $open_in_new_window=true) {
$text = ' ' . $text . ' ';
$new_win_txt = ($open_in_new_window) ? ' target="_blank"' : '';
// Hyperlink Class B domains *.(com|org|net|gov|edu|us|info|biz|ws|name|tv)(/*)
$text = preg_replace("#([\s{}\(\)\[\]])([A-Za-z0-9\-\.]+)\.(com|org|net|gov|edu|us|info|biz|ws|name|tv)((?:/[^\s{}\(\)\[\]]*[^\.,\s{}\(\)\[\]]?)?)#ie",
"'$1' . c2c_truncate_link(\"$2.$3$4\", \"$mode\", \"$trunc_before\", \"$trunc_after\") . ''",
$text);
// Hyperlink e-mail addresses
$text = preg_replace("#([\s{}\(\)\[\]])([A-Za-z0-9\-_\.]+?)@([^\s,{}\(\)\[\]]+\.[^\s.,{}\(\)\[\]]+)#ie",
"'$1' . c2c_truncate_link(\"$2@$3\", \"$mode\", \"$trunc_before\", \"$trunc_after\") . ''",
$text);
return substr($text,1,strlen($text)-2);
}
function c2c_truncate_link ($url, $mode='0', $trunc_before='', $trunc_after='...') {
if (1 == $mode) {
$url = preg_replace("/(([a-z]+?):\\/\\/[A-Za-z0-9\-\.]+).*/i", "$1", $url);
$url = $trunc_before . preg_replace("/([A-Za-z0-9\-\.]+\.(com|org|net|gov|edu|us|info|biz|ws|name|tv)).*/i", "$1", $url) . $trunc_after;
} elseif (($mode > 10) && (strlen($url) > $mode)) {
$url = $trunc_before . substr($url, 0, $mode) . $trunc_after;
}
return $url;
}
function convertLinks($text) {
$text = preg_replace("/(\r\n|\n|\r)/", "\n", $text);
$lines = explode("\n", $text);
for ($x = 0, $y = count($lines); $x < $y; $x++) {
$line = $lines[$x];
$words = explode(' ', $line);
for ($i = 0, $j = count($words); $i < $j; $i++) {
$word = $words[$i];
$punctuation = '.,\'")(<>;:'; // Links may not end in these
if (substr($word, 0, 7) == 'http://' || substr($word, 0, 4) == 'www.') {
$trailing = '';
// Knock off ending punctuation
$last = substr($word, -1);
while (strpos($punctuation, $last) !== false) {
// Last character is punctuation - eliminate it
$trailing .= $last;
$word = substr($word, 0, -1);
$last = substr($word, -1);
}
// Make link, add trailing punctuation back afterwards
$link = $word;
if (substr($link, 0, 4) == 'www.') {
// This link needs an http://
$link = 'http://'.$link;
}
$word = ''.str_replace('&', '&', $word).''.$trailing;
}
$words[$i] = $word;
}
$lines[$x] = implode(' ', $words);
}
return c2c_hyperlink_urls(implode("\n", $lines));
}
//Eksempel
echo convertLinks($forumpostInnhold);