php 用于多字节字符编码的ucfirst()函数

ebdffaop  于 2023-06-04  发布在  PHP
关注(0)|答案(8)|浏览(710)

I've asked about strtolower function.但是当使用外来字符时,它不会将它们转换为大写,所以我必须用途:

mb_strtolower($a,"utf8");

但是如果我想使用ucfirst()函数,我该怎么做呢?我还没有找到任何类似的功能,在那里我可以设置编码类型。

ncecgwcz

ncecgwcz1#

没有mb_ucfirst函数,正如您已经注意到的。你可以用两个mb_substr来伪造一个mb_ucfirst

function mb_ucfirst($string, $encoding)
{
    $firstChar = mb_substr($string, 0, 1, $encoding);
    $then = mb_substr($string, 1, null, $encoding);
    return mb_strtoupper($firstChar, $encoding) . $then;
}
lmyy7pcs

lmyy7pcs2#

这是更简洁的解决方案,尽管它与ucwords函数非常相似:

$final_string = mb_convert_case($your_string, MB_CASE_TITLE, 'UTF-8');

如果你需要大写字符串组成的一个词,这是最好的解决方案。

bvhaajcl

bvhaajcl3#

function mb_ucfirst($string)
{
    return mb_strtoupper(mb_substr($string, 0, 1)).mb_substr($string, 1);
}
g6ll5ycj

g6ll5ycj4#

截至2019-11-18,似乎stackoverflow上没有人做对了这一点,以下是mb_ucfirst()应该如何在userland中实现:
对于PHP>=8.0.0:

function mb_ucfirst(string $str, ?string $encoding = null): string
{
    return mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding) . mb_substr($str, 1, null, $encoding);
}

对于PHP >= 7.1.0:

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);
}

对于PHP>=5.1.0:

function mb_ucfirst($str, $encoding = null)
{
    if (!is_string($str)) {
        throw new InvalidArgumentException("Argument 1 must be string, " . gettype($str) . " given");
    }
    if ($encoding === null) {
        $encoding = mb_internal_encoding();
    } elseif (!is_string($encoding)) {
        throw new InvalidArgumentException("Argument 2 must be string|null, " . gettype($encoding) . " given");
    }
    return mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding) . mb_substr($str, 1, null, $encoding);
}
q5iwbnjs

q5iwbnjs5#

if (!function_exists('mb_ucfirst'))
{
    function mb_ucfirst($value)
    {
        return mb_strtoupper(mb_substr($value, 0, 1)) . mb_substr($value, 1);
    }
}
vi4fp9gy

vi4fp9gy6#

我在网页上使用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 );
}
fjnneemd

fjnneemd7#

/*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;
}
llew8vvj

llew8vvj8#

$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);

相关问题