Re: [SMARTY] capitalize modifier

From: Date: Thu, 24 Nov 2005 19:06:31 +0000
Subject: Re: [SMARTY] capitalize modifier
References: 1 2 3 4 5 6 7  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-13174@lists.php.net to get a copy of this message
Dep wrote:
mm> Smarty doesn't know that your string is "absolutely 100% correct mm> written English". and probably will never know. Sure, I understand that. But in such case it would be not a bad thing to at least include a note in SMARTY documentation, regarding that one particular case
+1. Contractions ending in n't and 's are extremely common in English, and they're definitely not word boundaries. So at least the English documentation should note this. Daniel Hedrick wrote:
I don't care which you fix, but it most certainly *is* a difference in behavior from the expectation.
I agree that it's a bug, but Smarty is Open Source, remember, so perhaps *you* can fix it yourself and then fill us in on what you did. My quick solution is to modify [smarty]/libs/plugins/modifier.capitalize.php. The _ char is not seen as a word boundary by PCRE, so here's a modified version of smarty_modifier_capitalize() that uses a quick str_replace to circumvent the n't and 's problem for those of us using Smarty with English text. Using a preg_replace to avoid the word-initial 's would be more elegant, but str_replace is much faster than preg_replace, so this works pretty well, or at least better than default behavior. modifier.capitalize.php: function smarty_modifier_capitalize($string, $uc_digits = false) {
    //replace common contraction endings with placeholders
    //that PCRE will not see as a word boundary
    $apos = array('n\'t ','\'s ','\'re ');
    $tmps = array('n__apos__t ','__apos__s ','__apos__re ');
    $string = str_replace($apos,$tmps,$string);
    //do the real work
    smarty_modifier_capitalize_ucfirst(null, $uc_digits);
    $string = preg_replace_callback('!\b\w+\b!', 'smarty_modifier_capitalize_ucfirst', $string);
    //restore the contractions and return
    return str_replace($tmps,$apos,$string);
} example.tpl: {assign value="max's horse didn't fall. 'someone' is coming. they're here!" var="capt"} {$capt|capitalize} Output: Max's Horse Didn't Fall. 'Someone' Is Coming. They're Here! -- Max Schwanekamp http://www.neptunewebworks.com/

« previous php.smarty.general (#13174) next »