Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

[modifier] ucnames to capitalize titles and places and stuff

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
krodelabestiole
Smarty n00b


Joined: 05 Jul 2011
Posts: 4

PostPosted: Tue Jul 05, 2011 9:31 pm    Post subject: [modifier] ucnames to capitalize titles and places and stuff Reply with quote

a bit like ucwords :
it's made to capitalise each word of a string except small words like prepositions or articles

ive been looking for such fonction on the web but as i found nothing that suited me here is what i wrote :

Code:
<?php

function smarty_modifier_ucnames($string) {
   
   $lang = 'fr';
   
   $no_cap['fr'] = array(
      'le','la','les','l','un','une','des','de','du','d',
      'à','a','au','aux','dans','par','pour','en','vers',
      'avec','sans','si','sur','sous','via','dès','chez',
      'ne','ou','mais','où','et','donc','ni','car','mon',
      'ton','son','ma','ta','sa','mes','tes','ses','notre',
      'votre','leur','nos','vos','leurs','me','te','se','y'
   );
   $no_cap['en'] = array(
      'of','a','the','and','an','or','nor','but','is','if',
      'then','else','when', 'at','from','by','on','off',
      'for','in','out','over','to','into','with','s'
   );
   
   $delimiter_1 = ' ';
   $delimiter_2 = '\'';
   $delimiter_3 = '-';
   
   $string = strtolower($string);
   $names = explode($delimiter_1,$string);
   foreach($names as &$name){
      $names_1 = explode($delimiter_2, $name);
      foreach($names_1 as &$name_1){
         $names_2 = explode($delimiter_3, $name_1);
         foreach($names_2 as &$name_2){
            if (!in_array($name_2,$no_cap[$lang])){
               $name_2 = ucfirst($name_2);
            }
         $name_1 = implode($delimiter_3, $names_2);
         }
      }
      $name = implode($delimiter_2, $names_1);
   }
   $string = implode($delimiter_1, $names);
   $string = ucfirst($string);
   return $string;
}

?>


this one can handle ' and -
it's made basically for the french text so you might have to modify it a bit for english titles (change $lang at least)


Last edited by krodelabestiole on Tue Jul 05, 2011 9:49 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
krodelabestiole
Smarty n00b


Joined: 05 Jul 2011
Posts: 4

PostPosted: Tue Jul 05, 2011 9:40 pm    Post subject: Reply with quote

btw what would be the better way to switch language ?

is there any smarty function to get the language or a php one such as getLocale ?
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
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