Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

modifier capitalize doesn't work properly for quoted text
Goto page Previous  1, 2
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Aug 23, 2004 9:40 pm    Post subject: Reply with quote

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 Sad
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Aug 23, 2004 9:43 pm    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Aug 23, 2004 9:53 pm    Post subject: Reply with quote

ok I got that fixed with a static var, not real pretty but better than a constant or global Wink
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Aug 23, 2004 10:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Aug 24, 2004 6:07 am    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Aug 24, 2004 7:13 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Aug 24, 2004 7:37 am    Post subject: Reply with quote

@messju: that's essentially what I was trying to suggest earlier. IMHO it is splitting hairs at this point Smile

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Aug 24, 2004 10:27 pm    Post subject: Reply with quote

I made the change to support ignore-words. Should I commit it?
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP