Question

For a project with Smarty, I want to use a title variable for an ID in HTML. The variable should be lowercase, shouldn't have whitespaces and vowel mutations (ä, ö, ü, é, è, à, ...). For example Übergrösse should be ubergrosse.

After a major search I can't find a really useful command. So I tried it with |replace modifier, like this:

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

Is there any better way to do this?

Était-ce utile?

La solution

I use this to clean text to generate safe urls, so it would probably work for what you need:

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

Save this code as modifier.safetext.php in your smarty plugins folder and then use it just like this:

{$title|safetext}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top