<?
//实现标题的正确大写 

//除了,a,an.the,bu,as if,and,or,nor,of,by之外,其它单词首字母全部大写 


function title_upcase($str) { 
    
//将全部单词首字大写 

    
$str = ucwords($str); 
    
//返回一个数组,包含字符串里的所有单词,并且以单词在字符串里的位置作为索引 

    
$wordlist = str_word_count($str,2); 
    
//排除数组里第一个和最后一个元素,因为不需要改变为小写 

    
$wordlist = array_slice($wordlist,1,-1,true); 
    
//如果包含下列单词,则全部小写 

    
foreach ($wordlist as $position => $word) { 
        
switch ($word) { 
            
case 'A': 
            
case 'An': 
            
case 'The': 
            
case 'But': 
            
case 'As': 
            
case 'If': 
            
case 'And': 
            
case 'Or': 
            
case 'Nor': 
            
case 'Of': 
            
case 'By': 
                
$lower = strtolower($word); 
                
$str{$position= $lower{0};                                 
        } 
    } 
    
return $str
}
?> 

 

函数描述及例子

 

<? 
$sample = "a study of interesteller galaxies as presented by scientist"
$upcased = title_upcase($sample); 
echo $sample// a study of interesteller galaxies as presented by scientist 

echo $upcased// A Study of Interesteller Galaxies as Presented by Scientist 
?> 

 

posted on 2009-11-03 11:29  钱途无梁  阅读(560)  评论(0)    收藏  举报