2014-01-28 36 views
0

对于Smarty项目,我想在HTML中使用标题变量作为ID。变量应该是小写的,不应该有空格和元音变异(ä,ö,ü,é,è,à,...)。 例如Übergrösse应该是ubergrosseEscape变量#id与Smarty友好

重大搜索后,我找不到一个非常有用的命令。所以我|replace修改试了一下,像这样:

<section id="{$title|lower|replace:' ':''|replace:'ä':'a'|replace:'ö':'o'|replace:'ü':'u'|replace:'é':'e'|replace:'è':'e'|replace:'à':'a'}">...</section> 

有没有什么更好的方法来做到这一点?

回答

1

我使用它来清洁文本生成安全网址,所以它可能会为你所需要的工作:

function smarty_modifier_safetext($string){ 
    $string = preg_replace("`\[.*\]`U","",$string); 
    $string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i','-',$string); 
    $string = preg_replace('`"`i', "", $string); 
    $string = htmlentities($string, ENT_COMPAT, 'utf-8'); 
    $string = preg_replace("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i","\\1", $string); 
    $string = html_entity_decode($string); 
    $string = preg_replace(array("`[^a-z0-9]`i","`[-]+`") , "-", $string); 
    return strtolower(trim($string, '-')); 
} 

将此代码另存modifier.safetext.php在智者的插件文件夹,然后用它就像这样:

{$title|safetext}