View previous topic :: View next topic |
Author |
Message |
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Aug 23, 2004 9:40 pm Post subject: |
|
|
I thought about a preg_match_all solution already, things get tricky when it comes time to replace the upper-cased words back into the original string. You can easily get borked results if a word is a subset of another word. We could keep track of word positions I suppose, but it seemed a bit much. preg_match_all can track matched positions as of 4.3.0, but that isn't early enough |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Aug 23, 2004 9:43 pm Post subject: |
|
|
Well, then perhaps leave it as preg_replace_callback, but have two versions: one that calls a function that assumes True for the param and one that assumes False.
eg:
[php:1:3778730b93]<?php
function smarty_modifier_capitalize($string, $uc_digits = false)
{
if ($uc_digits)
return preg_replace_callback('!\b\w+\b!', 'smarty_modifier_capitalize_ucfirst', $string);
else
return preg_replace_callback('!\b\w+\b!', 'smarty_modifier_capitalize_ucfirst_nodigs', $string);
}
?>[/php:1:3778730b93]
etc.
What a can of worms |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Aug 23, 2004 9:53 pm Post subject: |
|
|
ok I got that fixed with a static var, not real pretty but better than a constant or global |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Aug 23, 2004 10:03 pm Post subject: |
|
|
That's the one. I had thought of statics but completely forgot that they could be used outside of a class context!!
Thanks, Monte! |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Aug 24, 2004 6:07 am Post subject: |
|
|
I changed the docs to reflect the new optional parameter and updated the example to show the behaviour. I also re-tested with various word forms, whitespacing, linebreaks and character sets and it seems to work great.
Thanks again to both of you! |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Aug 24, 2004 7:13 am Post subject: |
|
|
instead of a static variable we could have two replace-functions and use them according to the value of $uc_digits .
[php:1:2f82f89b55]<?php
function smarty_modifier_capitalize($string, $uc_digits = false)
{
$callback = 'smarty_modifier_capitalize_uc' . ( $uc_digits ? 'digits' : 'first' );
return preg_replace_callback('!\b\w+\b!', $callback, $string);
}
function smarty_modifier_capitalize_ucfirst($string)
{
/* uppercase only words that do not contain any digits */
if (!preg_match('!\d!', $string[0]))
return ucfirst($string[0]);
else
return $string[0];
}
function smarty_modifier_capitalize_ucdigits($string)
{
/* uppercase all words */
return ucfirst($string[0]);
}
?>[/php:1:2f82f89b55] |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Aug 24, 2004 7:37 am Post subject: |
|
|
@messju: that's essentially what I was trying to suggest earlier. IMHO it is splitting hairs at this point
While we are taking the stick to this horse, if we want to add another nifty feature I propose allowing a second optional parameter which would be a comma separated list of words to ignore.
EG:
Code: | {$foo|capitalize:false:"the, of, an, and"} |
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Aug 24, 2004 10:27 pm Post subject: |
|
|
I made the change to support ignore-words. Should I commit it? |
|
Back to top |
|
|