krodelabestiole Smarty n00b
Joined: 05 Jul 2011 Posts: 4
|
Posted: Tue Jul 05, 2011 9:31 pm Post subject: [modifier] ucnames to capitalize titles and places and stuff |
|
|
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 |
|