- 论坛徽章:
- 0
|
1.10实现标题的正确大写
?php
//实现标题的正确大写
//除了,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
?>
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/51625/showart_415554.html |
|