ucwords没有大写重音字母

时间:2014-05-13 12:17:40

标签: php

我有一个大写字母的字符串。我使用ucwords()和mb_strtolower()函数只大写字符串的第一个字母。但是,当一个单词的第一个字母带有重音时,我会遇到一些问题。例如:

ucwords(mb_strtolower('GRANDE ÁRVORE')); //outputs 'Grande árvore'

为什么第二个单词的第一个字母没有被大写?我该怎么做才能解决这个问题?

2 个答案:

答案 0 :(得分:13)

ucwords是PHP的核心功能之一,它非常无视非ASCII或非Latin-1编码。*对于处理多字节字符串和/或非ASCII字符串,您应该使用多字节识别mb_convert_case

mb_convert_case($str, MB_CASE_TITLE, 'UTF-8')
// your string encoding here --------^^^^^^^

*我不完全确定它是仅适用于ASCII还是至少使用Latin-1,但我甚至都不愿意找到它。

答案 1 :(得分:-1)

ucwords无法识别重音字符。尝试使用mb_convert_case

$str = 'GRANDE ÁRVORE';

function ucwords_accent($string)
{
    if (mb_detect_encoding($string) != 'UTF-8') {
        $string = mb_convert_case(utf8_encode($string), MB_CASE_TITLE, 'UTF-8');
    } else {
        $string = mb_convert_case($string, MB_CASE_TITLE, 'UTF-8');
    }
    return $string;
}

echo ucwords_accent($str);