大写字符串中每个单词的第一个字符,除了'和','到'等

时间:2011-01-02 21:49:11

标签: php

如何将字符串中的每个单词的第一个字符设为大写字母,接受一些我不想转换它们的单词,比如 - 和,等等?

例如,我希望这个 - ucwords('art and design')输出下面的字符串,

'艺术与设计'

是否可以像 - strip_tags($text, '<p><a>')我们允许

并在字符串中?

还是我应该用别的东西?请指教!

感谢。

5 个答案:

答案 0 :(得分:17)

这些都不是真的 UTF8友好,所以这里是完美无缺的(到目前为止)

function titleCase($string, $delimiters = array(" ", "-", ".", "'", "O'", "Mc"), $exceptions = array("and", "to", "of", "das", "dos", "I", "II", "III", "IV", "V", "VI"))
{
    /*
     * Exceptions in lower case are words you don't want converted
     * Exceptions all in upper case are any words you don't want converted to title case
     *   but should be converted to upper case, e.g.:
     *   king henry viii or king henry Viii should be King Henry VIII
     */
    $string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
    foreach ($delimiters as $dlnr => $delimiter) {
        $words = explode($delimiter, $string);
        $newwords = array();
        foreach ($words as $wordnr => $word) {
            if (in_array(mb_strtoupper($word, "UTF-8"), $exceptions)) {
                // check exceptions list for any words that should be in upper case
                $word = mb_strtoupper($word, "UTF-8");
            } elseif (in_array(mb_strtolower($word, "UTF-8"), $exceptions)) {
                // check exceptions list for any words that should be in upper case
                $word = mb_strtolower($word, "UTF-8");
            } elseif (!in_array($word, $exceptions)) {
                // convert to uppercase (non-utf8 only)
                $word = ucfirst($word);
            }
            array_push($newwords, $word);
        }
        $string = join($delimiter, $newwords);
   }//foreach
   return $string;
}

用法:

$s = 'SÃO JOÃO DOS SANTOS';
$v = titleCase($s); // 'São João dos Santos' 

答案 1 :(得分:7)

因为我们都喜欢regexps,一种替代方案,也适用于interpunction(与explode(" ",...)解决方案不同)

$newString = preg_replace_callback("/[a-zA-Z]+/",'ucfirst_some',$string);

function ucfirst_some($match)
{
    $exclude = array('and','not');
    if ( in_array(strtolower($match[0]),$exclude) ) return $match[0];
    return ucfirst($match[0]);
}

修改已添加strtolower(),或“不是”将保持“不”。

答案 2 :(得分:3)

你必须使用ucfirst并遍历每个单词,例如检查每个例外的一系列例外。

如下所示:

$exclude = array('and', 'not');
$words = explode(' ', $string);
foreach($words as $key => $word) {
    if(in_array($word, $exclude)) {
        continue;
    }
    $words[$key] = ucfirst($word);
}
$newString = implode(' ', $words);

答案 3 :(得分:3)

我知道问题已经过去几年了,但我正在寻找一个能够在我编程的CMS标题中保证正确英语的答案,并从本页的想法中写出了轻量级函数,所以我想我愿意分享一下:

function makeTitle($title){
    $str = ucwords($title);     
    $exclude = 'a,an,the,for,and,nor,but,or,yet,so,such,as,at,around,by,after,along,for,from,of,on,to,with,without';        
    $excluded = explode(",",$exclude);
    foreach($excluded as $noCap){$str = str_replace(ucwords($noCap),strtolower($noCap),$str);}      
    return ucfirst($str);
}

排除名单见于: http://www.superheronation.com/2011/08/16/words-that-should-not-be-capitalized-in-titles/

USAGE: makeTitle($title);

答案 4 :(得分:2)

这个怎么样?

$string = str_replace(' And ', ' and ', ucwords($string));