【问题标题】:Escape variable #id friendly with Smarty转义变量#id 对 Smarty 友好
【发布时间】:2014-02-20 10:14:41
【问题描述】:

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

经过大量搜索后,我找不到真正有用的命令。所以我用|replace修饰符试了一下,像这样:

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

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

【问题讨论】:

    标签: html internationalization smarty diacritics lowercase


    【解决方案1】:

    我用它来清理文本以生成安全的 url,所以它可能会满足你的需要:

    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, '-'));
    }
    

    将此代码保存为您的 smarty 插件文件夹中的 modifier.safetext.php,然后像这样使用它:

    {$title|safetext}
    

    【讨论】:

      猜你喜欢
      • 2021-04-07
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      相关资源
      最近更新 更多