字符串大小写正确

Moh*_*din -3 php libraries

我有数千个字符串,我想正确地大写它们

默认字符串大小写可以更改“world war ii”->“World War II”

或者

“usa”->“Usa”还有其他类型的智能大写解决方案吗?

小智 5

我不知道为什么你的问题被否决。不管怎样,请看下面的功能并根据您的要求进行调整

function titleCase($string) 
{
    $word_splitters = array(' ', '-', "O'", "L'", "D'", 'St.', 'Mc');
    $lowercase_exceptions = array('the', 'van', 'den', 'von', 'und', 'der', 'de', 'da', 'of', 'and', "l'", "d'");
    $uppercase_exceptions = array('III', 'IV', 'VI', 'VII', 'VIII', 'IX');

    $string = strtolower($string);
    foreach ($word_splitters as $delimiter)
    { 
        $words = explode($delimiter, $string); 
        $newwords = array(); 
        foreach ($words as $word)
        { 
            if (in_array(strtoupper($word), $uppercase_exceptions))
                $word = strtoupper($word);
            else
            if (!in_array($word, $lowercase_exceptions))
                $word = ucfirst($word); 

            $newwords[] = $word;
        }

        if (in_array(strtolower($delimiter), $lowercase_exceptions))
            $delimiter = strtolower($delimiter);

        $string = join($delimiter, $newwords); 
    } 
    return $string; 
}
Run Code Online (Sandbox Code Playgroud)

最初提到@这里

希望这可以帮助。干杯!