php 用于多字节字符编码的 ucfirst() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2517947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ucfirst() function for multibyte character encodings
提问by Simon
I've asked about strtolowerfunction.But when using foreign characters it doesn't convert them into uppercase, so I must use:
我问过strtolower功能。但是当使用外来字符时,它不会将它们转换为大写,所以我必须使用:
mb_strtolower($a,"utf8");
But what can I do, if I want to use ucfirst()function? I haven't found any similar function, where I can set encoding type.
但是,如果我想使用ucfirst()功能,我该怎么办?我还没有找到任何类似的功能,我可以在其中设置编码类型。
回答by zneak
There is no mb_ucfirstfunction, as you've already noticed. You can fake a mb_ucfirstwith two mb_substr:
mb_ucfirst正如您已经注意到的那样,没有任何功能。你可以mb_ucfirst用两个来伪造一个mb_substr:
function mb_ucfirst($string, $encoding)
{
$strlen = mb_strlen($string, $encoding);
$firstChar = mb_substr($string, 0, 1, $encoding);
$then = mb_substr($string, 1, $strlen - 1, $encoding);
return mb_strtoupper($firstChar, $encoding) . $then;
}
回答by Alex Belyaev
This is more concise solution, although it is rather similar to ucwordsfunction:
这是更简洁的解决方案,尽管它与ucwords功能相当相似:
$final_string = mb_convert_case($your_string, MB_CASE_TITLE, 'UTF-8');
If you need to capitalize string consist of one word, it is the best solution.
如果您需要将一个单词组成的字符串大写,这是最好的解决方案。
回答by Koralek M.
function mb_ucfirst($string)
{
return mb_strtoupper(mb_substr($string, 0, 1)).mb_strtolower(mb_substr($string, 1));
}
回答by goyote
if (!function_exists('mb_ucfirst'))
{
function mb_ucfirst($value)
{
return mb_strtoupper(mb_substr($value, 0, 1)) . mb_substr($value, 1);
}
}
回答by hanshenrik
as of 2019-11-18, it seems nobody on stackoverflow got this right, here's how mb_ucfirst() should be implemented in userland:
截至 2019 年 11 月 18 日,似乎 stackoverflow 上没有人做到这一点,以下是 mb_ucfirst() 应如何在用户空间中实现:
function mb_ucfirst(string $str, string $encoding = null): string
{
if ($encoding === null) {
$encoding = mb_internal_encoding();
}
return mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding) . mb_substr($str, 1, null, $encoding);
}
回答by Jaroslav ?treit
I′m using cp1250 on webpage, and for ú mb_ucfirst doesn′t work, so little upgrade:
我在网页上使用 cp1250,对于 ú mb_ucfirst 不起作用,所以很少升级:
function mb_ucfirst($string)
{
$main_encoding = "cp1250";
$inner_encoding = "utf-8";
$string = iconv($main_encoding, $inner_encoding , $string );
$strlen = mb_strlen($string);
$firstChar = mb_substr($string, 0, 1, $inner_encoding);
$then = mb_substr($string, 1, $strlen - 1, $inner_encoding);
return $string = iconv($inner_encoding, $main_encoding , mb_strtoupper($firstChar, $inner_encoding) . $then );
}
回答by user3302248
/*This worked correctly for me*/
function mb_ucfirst($string, $encoding='UTF-8')
{
$firstChar = mb_substr($string, 0, 1, $encoding);
$then = mb_substr($string, 1, mb_strlen($string, $encoding)-1, $encoding);
return mb_strtoupper($firstChar, $encoding) . $then;
}
回答by Paris Z
$string = trim(preg_replace('/\s+/', ' ', $string));
$string_ar = explode(' ', mb_strtolower($string,'utf-8'));
foreach($string_ar as $key => $value {
$string_str .= mb_convert_case(mb_substr(trim($value), 0, 1), MB_CASE_TITLE, 'utf-8')
. mb_substr(trim($value),1)
. ' ';
}
$string = trim($string_str);

