2010-12-15 4 views
1

Y a-t-il un modificateur smarty qui ajoutera des points d'ancrage aux liens. par exempleModificateur Smarty - Transformez les URL en liens

$smarty->assign('mytext','This is my text with a http://www.link.com'); 

{$mytext|link} 

qui affiche,

This is my text with a <a href='http://www.link.com'>http://www.link.com</a> 

Répondre

3

J'ai créé ce modificateur, semble fonctionner assez bien. Je pense que la plus grande amélioration pourrait être à l'expression rationnelle.

<?php 
/** 
* Smarty plugin 
* @package Smarty 
* @subpackage PluginsModifier 
*/ 

/** 
* Smarty link_urls plugin 
* 
* Type:  modifier<br> 
* Name:  link_urls<br> 
* Purpose: performs a regex and replaces any url's with links containing themselves as the text 
* This could be improved by using a better regex. 
* And maybe it would be better for usability if the http:// was cut off the front? 
* @author Andrew 
* @return string 
*/ 

function smarty_modifier_link_urls($string) 
{ 
    $linkedString = preg_replace_callback("/\b(https?):\/\/([-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]*)\b/i", 
           create_function(
           '$matches', 
           'return "<a href=\'".($matches[0])."\'>".($matches[0])."</a>";' 
           ),$string); 

    return $linkedString; 
} 

?> 
+1

Vous pouvez juste retour la valeur directement à partir de la fonction 'preg_replace_callback', cela vous évitera de définir une variable supplémentaire. – RobertPitt

0

Aussi, vous pouvez utiliser Smarty Modificateur Variable "regex_replace":

{$variable|regex_replace:"/\b((https?):\/\/([-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]*))\b/i":"<a href='$1' target='_blank'>$3</a>"} 
1

Essayez cette solution ses travaux pour toutes les urls (https, http et www)

{$customer.description|regex_replace:" @((([[:alnum:]]+)://|www\.)([^[:space:]]*)([[:alnum:]#?/&=]))@": 
" <a href=\"\\1\" target=\"_blank\" >\\1</a>"} 
+0

C'est une solution tellement bizarre, mais ça marche vraiment: D – Erikas

Questions connexes