2014-01-28 83 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}