All pastes #202474 Raw Edit

arex

public php v1 · immutable
#202474 ·published 2006-10-14 15:09 UTC
rendered paste body
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<a href=\"http://$2.$3$4\" title=\"http://$2.$3$4\"$new_win_txt>' . c2c_truncate_link(\"$2.$3$4\", \"$mode\", \"$trunc_before\", \"$trunc_after\") . '</a>'",        $text);    // Hyperlink e-mail addresses    $text = preg_replace("#([\s{}\(\)\[\]])([A-Za-z0-9\-_\.]+?)@([^\s,{}\(\)\[\]]+\.[^\s.,{}\(\)\[\]]+)#ie",        "'$1<a href=\"mailto:$2@$3\" title=\"mailto:$2@$3\">' . c2c_truncate_link(\"$2@$3\", \"$mode\", \"$trunc_before\", \"$trunc_after\") . '</a>'",        $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 = '<a href="'.str_replace('&', '&amp;', $link).'">'.str_replace('&', '&amp;', $word).'</a>'.$trailing;			}			$words[$i] = $word;		}		$lines[$x] = implode(' ', $words);	}	return c2c_hyperlink_urls(implode("\n", $lines));}//Eksempelecho convertLinks($forumpostInnhold);