Seite 1 von 4

Link kürzen

Verfasst: 05.03.2004 22:56
von schurl
Hallo, nachdem ich grad 2 stunden rum gedocktert habe, brauch ich nun doch Hilfe von den Profis.

ich versuch bei geposteten Links ohne [\url] die Zeichen zu begrenzen. aber irgendwie geht das nicht.
Ich bin in bbcode.php - function make_clickable()

irgendwie muss ich doch hier den link (angezeigter link) kürzen können (50 Zeichen)

Code: Alles auswählen

$ret = preg_replace("#([\t\r\n ])(www|ftp)\.(([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)#i", '\1<a href="extern.php?http://\2.\3" target="_blank">\2.\3</a>', $ret);
Danke

Verfasst: 06.03.2004 17:12
von schurl
Ich hab nun was getestet, schaut schon gut aus.
Der link wird nun nach 50 Zeichen abgebrochen, und ein … wird angehängt.

Code: Alles auswählen

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, comma, double quote or <
	$ret = preg_replace ("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#ie", "'\\1<a href=\"extern.php?\\2\" target=\"_blank\">'.urlpars('\\2').'</a>'", $ret);

	// matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing
	// Must contain at least 2 dots. xxxx contains either alphanum, or "-"
	// zzzz is optional.. will contain everything up to the first space, newline, 
	// comma, double quote or <.
  

	// 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 ])((www|ftp)\.[^ \"\t\n\r<]*)#ie", "'\\1<a href=\"extern.php?http://\\2\" target=\"_blank\">'.urlpars('\\2').'</a>'", $ret);


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

	return($ret);
}

function urlpars($tmp){

	if(strlen($tmp)>= 50)
		$tmp = substr($tmp, 0, 30).'...';

	return($tmp);
}
Links schauen jetzt so aus:
http://www.embjapan.de/posting.php?mode=editpost&p...

was könnte mann da noch machen?

Verfasst: 06.03.2004 21:04
von Elo

Code: Alles auswählen

$ret = preg_replace("/<a href=(.*?)>(.*?)<\/a>/ie", "(strlen(\"\\2\") > 65 && !eregi(\"<\", \"\\2\") ) ? '<a href='.stripslashes(\"\\1\").'>'.substr(\"\\2\", 0, 35) . '.....' . substr(\"\\2\", -25).'</a>' : '<a href='.stripslashes(\"\\1\").'>'.\"\\2\".'</a>'", $ret);

Verfasst: 06.03.2004 23:18
von schurl
Das ist gut°°
Danke!

Verfasst: 07.03.2004 03:17
von AmigaLink
Sorry, ich steh da etwas auf dem schlauch. :-?
Wo muss die Zeile hin?

Verfasst: 07.03.2004 10:38
von schurl
in ./includes/bbcode.php
suche nach:

Code: Alles auswählen

	// Remove our padding..
	$ret = substr($ret, 1);
davor einfügen:

Code: Alles auswählen

	$ret = preg_replace("/<a href=(.*?)>(.*?)<\/a>/ie", "(strlen(\"\\2\") > 65 && !eregi(\"<\", \"\\2\") ) ? '<a href='.stripslashes(\"\\1\").'>'.substr(\"\\2\", 0, 35) . '.....' . substr(\"\\2\", -25).'</a>' : '<a href='.stripslashes(\"\\1\").'>'.\"\\2\".'</a>'", $ret);

Verfasst: 07.03.2004 11:13
von AmigaLink
Danke! :)

Verfasst: 17.08.2005 08:18
von baerenwurm
Bei mir sind's immer noch ca. 5 Zeichen zuviel - was muss ich denn dann ändern?

Verfasst: 04.09.2005 02:36
von ll-rooth
Die Lösung liegt oft so nah...
Ich hab ewig nach einer Lösung gesucht bis ich diesen Thread hier gefunden hab.

Danke :)

Verfasst: 16.10.2005 20:26
von mgutt
was macht der code?

langerlink...

oder

langer...link

?

ich hätte gerne die 2. variante, damit man noch die datei am ende erkennen kann. einmal aus sucherheitsgründen und einmal, damit der user direkt sehen kann, welche datei ihn erwartet.