PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Links automatisch umwandeln und beschneiden



der_rokko
04-02-2006, 10:33
--- ERLEDIGT ---

Hallo zusammen!

Folgendes Problem:
Ich habe eine Homepage wo sich User so ähnlich wie in Gästebüchern eintragen können. Natürlich möchte ich da Links automatisch umwandeln lassen. Bisher machte ich das mit der Funktion make_clickable() aus dem phpBB-Forum.
Leider gibts nun aber Links, die sehr lange sind und mir das ganze Layout der Homepage versauen. Ich möchte nun eine Funktion wie in den vBulletin-Boards, die lange Links automatisch kürtzt.

Beispiel:
http://www.ricardo.ch/cgi-bin/auk?lng=de;cmd=viewlot;lotid=398052022;OrderBy=Clo seTime;SortOrder=1

Gleichzeitig möchte ich sehr lange Wörter (> 100 Zeichen) automatisch umbrechen lassen (ja ich möchte solche "Wörter" zulassen, es ist eine Homepage für Freunde, da kann gut und gerne mal jemand an den Tasten kleben bleiben ;) ). Leider habe ich es bis jetzt noch nicht geschafft, beide Funktionen zu vereinen bzw. nur sehr schlecht.


Meine Funktionen sehen so aus (falls diese für jemanden interessant sind), ich möchte sie aber am liebsten komplett ersetzen (und ja ich weiss, quick n' dirty):


/**
Prepares a message string
*/
function prepare_string($text) {

return nl2br(make_clickable(add_breaks($text, 70)));
}

/**
Make links clickable (thanks phpBB)
*/
function make_clickable($text)
{

// pad it with a space so we can match things at the start of the 1st line.
$ret = " " . $text;

// matches an "xxxx://yyyy" URL at the start of a line, or after a space.
// xxxx can only be alpha characters.
// yyyy is anything up to the first space, newline, or comma.
$ret = preg_replace("#([\n ])([a-z]+?)://([^,\t \n\r]+)#i", "\\1<a href=\"\\2://\\3\">\\2://\\3</a>", $ret);

// matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing
// Must contain at least 2 dots. xxxx contains either alphanum, or "-"
// yyyy contains either alphanum, "-", or "."
// zzzz is optional.. will contain everything up to the first space, newline, or comma.
// This is slightly restrictive - it's not going to match stuff like "forums.foo.com"
// This is to keep it from getting annoying and matching stuff that's not meant to be a link.
$ret = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^,\t \n\r]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\">www.\\2.\\3\\4</a>", $ret);

// matches an email@domain type address at the start of a line, or after a space.
// Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
$ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)?[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);

// Remove our padding..
$ret = substr($ret, 1);

return($ret);
}


/**
After $length a new <br /> will be added. It truncates the words into smaller pieces.
*/
function add_breaks($str, $length) {
$str = html_to_utf8($str);
$words = explode(' ', $str);

$new = '';
foreach($words as $i) {
if( strlen($i) <= $length OR preg_match("#([\n ])([a-z]+?)://([^,\t \n\r]+)#i", $i) != 0 OR preg_match("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^,\t \n\r]*)?)#i", $i) != 0 OR preg_match("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)?[\w]+)#i", $i) != 0 ) {
$new .= $i;
}
else {
while( strlen($i) > $length ) {
$new .= substr($i, 0, $length) ."\n";
$i = substr($i, $length);
}
$new .= $i;
}
$new .= ' ';
}

return trim(utf8_to_html($new));

}

/**
Workaround for broken PHP function htmlentities() with UTF-8
*/
function html_to_utf8($str) {
return str_replace(array('&amp;', '&ouml;', '&auml;', '&uuml;', '&Ouml;', '&Auml;', '&Uuml;'), array('&', 'ö', 'ä', 'ü', 'Ö', 'Ä', 'Ü'), $str);
}
/**
Workaround for broken PHP function html_entitie_decode() with UTF-8
*/
function utf8_to_html($str) {
return str_replace(array('&', 'ö', 'ä', 'ü', 'Ö', 'Ä', 'Ü'), array('&amp;', '&ouml;', '&auml;', '&uuml;', '&Ouml;', '&Auml;', '&Uuml;'), $str);
}

Ich hoffe, jemand hat gerade ein solches Codeschnipsel parat und könnte mir das überlassen :)
Vielen Dank euch allen!
Roman



--- ERLEDIGT ---
Ich habe nun die Funktion umgeschrieben und anstatt mit Regex arbeite ich nun die Umwandlung in einer bereits vorhandenen Schleife ab, wo alle Wörter einzeln betrachtet werden. Funktioniert bestens.