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

lau*_*kok 12 php

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

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

'艺术与设计'

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

在字符串中?

或者我应该用别的东西?请指教!

谢谢.

Ant*_*Max 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;
}
Run Code Online (Sandbox Code Playgroud)

用法:

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


mvd*_*vds 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]);
}
Run Code Online (Sandbox Code Playgroud)

编辑添加strtolower(),或"不"将保持"不".


fre*_*oma 5

您将不得不使用 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);
Run Code Online (Sandbox Code Playgroud)


ajr*_*eal 5

这个怎么样 ?

$string = str_replace(' And ', ' and ', ucwords($string));
Run Code Online (Sandbox Code Playgroud)


pet*_*duc 5

我知道问题已经过去几年了,但我一直在寻找一个答案,以确保我正在编程的 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);
}
Run Code Online (Sandbox Code Playgroud)

排除列表位于: http://www.superheronation.com/2011/08/16/words-that-should-not-be-capitalized-in-titles/

USAGE: makeTitle($title);
Run Code Online (Sandbox Code Playgroud)