0

I'm trying to use ucfirst () to capitalize the first letter of a string but when the string contains éàçîô... it won't change it.

String

GÉNIE CIVIL

Should return

Génie civil

but return instead

GÉnie civil

As you can see, it ignored the second letter "É" to be lowercased. I know it's some king of encoding (I'm using UTF-8). Does anyones know how to fix this ?

Edit

I don't have mbstring enable, any other solutions ?

4

1 回答 1

1

首先 ucfirst() 做它所说的,它大写第一个字符。因此第二个字符和其他字符不会像你想要的那样调整。您需要 strtolower() 另一个字符(这样做时可能会遇到特殊字符的类似问题,但请进一步看,我在示例中也为您提供了答案)。

现在对于将使用本机 php 函数(如果存在)或模拟它的 mb_ucfirst(),您可以在它们不存在时定义自己的 mb_strtoupper() 和 mb_substr(),但对于这个问题,我将所有内容都包装在一个函数中。

所以我推荐的功能是:

<?php 
if (!function_exists('mb_ucfirst'))
{
    function mb_ucfirst($string)
    {
        if (function_exists('mb_strtoupper') && function_exists('mb_substr'))
        {
            return mb_strtoupper(mb_substr($string, 0, 1), 'UTF-8').mb_substr($string, 1);
        }
        else
        {
            // Credit to Quicker at http://php.net/manual/en/function.ucfirst.php
            // If it does not work, replace it with another utf8 ucfirst function
            if ($string{0} >= "\xc3")
            {
                return ($string{1} >= "\xa0"
                        ? ($string{0}.chr(ord($string{1})-32))
                        : ($string{0}.$string{1})).substr($string, 2);
            }
            return ucfirst($string);
        }
    }
}
?>

补充说明

您可能还对该库感兴趣:https ://github.com/fluxbb/utf8——大多数编码函数在 /core/native.php 中,在 /functions 中有 ucfirst() 和其他函数。那个可能比我的更受考验,甚至认为我喜欢我的小号只满足你的需要。

具有许多不同功能的完整示例

警告:如果您没有 mb_string 模块,您可能需要稍微调整一下代码。

<?php
mb_internal_encoding('UTF-8'); // make sure internal encoding is in UTF-8

function escape_html($string)
{
    $charset = 'UTF-8';
    return htmlspecialchars($string, ENT_QUOTES, $charset);
}
/*
function mb_ucfirst($str)
{
    $currentEncoding = mb_internal_encoding();
    mb_internal_encoding('UTF-8');
    preg_match_all("~^(.)(.*)$~u", $str, $arr);
    $str = mb_strtoupper($arr[1][0]).$arr[2][0];
    mb_internal_encoding($currentEncoding);
    return $str;
}*/
if (!function_exists('mb_ucfirst'))
{
    function mb_ucfirst($string)
    {
        if (function_exists('mb_strtoupper') && function_exists('mb_substr'))
        {
            return mb_strtoupper(mb_substr($string, 0, 1), 'UTF-8').mb_substr($string, 1);
        }
        else
        {
            // Credit to Quicker at http://php.net/manual/en/function.ucfirst.php
            // If it does not work, replace it with your favorite utf8 ucfirst function
            if ($string{0} >= "\xc3")
            {
                return ($string{1} >= "\xa0"
                        ? ($string{0}.chr(ord($string{1})-32))
                        : ($string{0}.$string{1})).substr($string, 2);
            }
            return ucfirst($string);
        }
    }
}
$testTexts = array(
    'GÉNIE CIVIL',
    'gÉnie civil',
    'Génie civil',
    'GENIE CIVIL',
    'Epinard',
    'ÉPinard',
    'épinard',
    'epinard',
    'é',
    'Ç',
    'ç',
);

// Credit to leha_grobov php.net/strtolower, I inverted it ;-)
function strtoupper_utf8($string)
{ 
  $convert_from = array( 
    "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 
    "v", "w", "x", "y", "z", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", 
    "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "ø", "ù", "ú", "û", "ü", "ý", "а", "б", "в", "г", "д", "е", "ё", "ж", 
    "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы", 
    "ь", "э", "ю", "я" 
  ); 
  $convert_to = array( 
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", 
    "V", "W", "X", "Y", "Z", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", 
    "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж", 
    "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ъ", 
    "Ь", "Э", "Ю", "Я" 
  ); 

  return str_replace($convert_from, $convert_to, $string); 
} 

// Credit to leha_grobov php.net/strtolower, I twicked it ;-)
function ucfirst_utf8_1($string)
{ 
  $convert_from = array( 
    "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 
    "v", "w", "x", "y", "z", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", 
    "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "ø", "ù", "ú", "û", "ü", "ý", "а", "б", "в", "г", "д", "е", "ё", "ж", 
    "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы", 
    "ь", "э", "ю", "я" 
  ); 
  $convert_to = array( 
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", 
    "V", "W", "X", "Y", "Z", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", 
    "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж", 
    "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ъ", 
    "Ь", "Э", "Ю", "Я" 
  );

  return str_replace($convert_from, $convert_to, mb_substr($string, 0, 1)).mb_substr($string, 1); 
} 

// Credit to Quicker at http://php.net/manual/en/function.ucfirst.php
function ucfirst_utf8_2($stri)
{ 
    if($stri{0}>="\xc3")
    {
        return (($stri{1}>="\xa0")
            ? ($stri{0}.chr(ord($stri{1})-32))
            : ($stri{0}.$stri{1})).substr($stri,2);
    }
    return ucfirst($stri);
}

?>
<!DOCTYPE html>
<html>
<head>
<title>Test ucfirst()</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style>
table {
    border-collapse: collapse;
}
td, th {
    border: 1px solid black;
    padding: 5px;
}
</style>
</head>
<body>
<table>
        <tr>
            <th>$t</th>
            <th>ucfirsdt($t)</th>
            <th>ucfirst(strtolower($t)</th>
            <th>mb_ucfirst($t)</th>
            <th>strtoupper_utf8($t)</th>
            <th>ucfirst_utf8_1($t);</th>
            <th>ucfirst_utf8_2($t);</th>
        <tr>
    <?php foreach ($testTexts as $t): ?>
        <tr>
            <td><?php echo escape_html($t); ?></td>
            <td><?php echo escape_html(ucfirst($t)); ?></td>
            <td><?php echo escape_html(ucfirst(strtolower($t))); ?></td>
            <td><?php echo escape_html(mb_ucfirst($t)); ?></td>
            <td><?php echo escape_html(strtoupper_utf8($t)); ?></td>
            <td><?php echo escape_html(ucfirst_utf8_1($t)); ?></td>
            <td><?php echo escape_html(ucfirst_utf8_2($t)); ?></td>
        </tr>
    <?php endforeach; ?>    
</table>
</body>
</html>
于 2013-06-27T00:19:02.157 回答