1

リンクにアンカータグを追加する賢い修飾子はありますか?例えば

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

{$mytext|link}

表示されます、

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

4 に答える 4

4

私はこの修飾子を作成しましたが、かなりうまく機能しているようです。最大の改善は正規表現にあると思います。

<?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;
} 

?>
于 2010-12-16T00:17:46.360 に答える
1

このソリューションを試してください。すべてのURL(https、http、およびwww)で機能します。

{$customer.description|regex_replace:" @((([[:alnum:]]+)://|www\.)([^[:space:]]*)([[:alnum:]#?/&=]))@":
 " <a href=\"\\1\" target=\"_blank\" >\\1</a>"}
于 2015-09-20T18:57:33.160 に答える
0

また、Smarty VariableModifier"regex_replace"を使用することもできます。

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

プラグインを作成する必要があります。

http://www.smarty.net/docsv2/en/

于 2010-12-15T17:02:44.577 に答える