Página 1 de 1

Acortar enlaces demasiado largos

Publicado: 16 Feb 2006, 19:01
por ThE KuKa
Con estas modificaciones se deberían en teoría acortar los links que son demasiado largos a nuestro gusto.
Sirve tanto para los links que se generan automáticamente con la función make_clickable cómo para los que se generan usando bbcode.

Lo primero que debemos de hacer para cambiar los generados con la función make_clickable es abrir el script includes/bbcode.php (lin +/- 619) y localizar la función make_clickable

Código: Seleccionar todo

function make_clickable($text)
{
	$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $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<]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\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 <.
	$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</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);
}
Y sustituirla por esta otra:

Código: Seleccionar todo

function make_clickable($text)
{
	$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);
	$maximo=40;
	// 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<]*)#ise", "'\\1<a href=\"\\2\" target=\"_blank\"  title=\"\\2\">'.(strlen('\\2')>=$maximo ? substr('\\2',0,$maximo).'...':'\\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 <.
	$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://\\2\" target=\"_blank\"  title=\"\\2\">'.(strlen('\\2')>=$maximo ? substr('\\2',0,$maximo).'...':'\\2').'</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]+)#ie", "'\\1<a href=\"mailto:\\2@\\3\">'.(strlen('\\2@\\3')>=$maximo ? substr('\\2@\\3',0,$maximo).'...':'\\2@\\3').'</a>'", $ret);

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

	return($ret);
}
Dónde $maximo se debe de configurar como el número máximo de caracteres a partir del cual se cortará.

Bien ahora se trata de hacer lo mismo para los mensajes de bbcode, y resulta mucho más sencillo hacer con una funcioncilla en javascript que cargaremos en el head del foro.

Para ello abrimos el archivo templates/subSilver/overall_header.tpl y localizamos la línea:

Código: Seleccionar todo

<!-- END switch_enable_pm_popup -->
Y después añadimos nuestra función:

Código: Seleccionar todo

<script language="Javascript" type="text/javascript">
<!--
function acorta_url(texto)
{

	if (texto.length>40)
	{
		texto=texto.substr(0,40)+"...";
	}
	document.write(texto);
}
//-->
</script>
En esta funcioncita teneis que sustituir el 40 por el numero de caracteres a partir del cual se acortarán los enlaces.

Una vez que hemos añadido en el head del foro la función ya la podemos usar en la plantilla del bbcode. Abrimos el archivo templates/subSilver/bbcode.tpl y localizamos al final del todo el siguiente codígo:

Código: Seleccionar todo

<!-- BEGIN url --><a href="{URL}" target="_blank" class="postlink">{DESCRIPTION}</a><!-- END url -->

<!-- BEGIN email --><a href="mailto:{EMAIL}">{EMAIL}</A><!-- END email -->
Y lo sustituimos por:

Código: Seleccionar todo

<!-- BEGIN url --><a href="{URL}" title="{URL}" target="_blank" class="postlink">
<script language="JavaScript" type="text/javascript">
acorta_url("{DESCRIPTION}");
</script>
</a><!-- END url -->

<!-- BEGIN email --><a href="mailto:{EMAIL}">
<script language="JavaScript" type="text/javascript">
acorta_url("{EMAIL}");
</script>
</A><!-- END email -->
Con esto debería de funcionar y no dar problemas, en realidad es mejor usar javascript para estas cosas si sólo afecta en el cliente cómo es el caso, pero para la función make_clickable hemos tenido que cambiar la expresiones regulares.

Si alguién lo quiere probar, a mi me ha funcionado, sólo decir que lo que hace es cortar el enlace, es decir no hace maravillas de arreglarlo para que por ejemplo en una dirección larga corte por en medio y se vea la página final.