php 使数组中的每个第一个字符都大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3315475/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:18:51  来源:igfitidea点击:

Make first every first character uppercase in array

phparrays

提问by Victor Bjelkholm

I'm trying to get all my first characters in a PHP array to be uppercase.

我正在尝试将 PHP 数组中的所有第一个字符都设为大写。

PHP code:

PHP代码:

<?php
$ordlista = file_get_contents('C:/wamp/www/bilder/filmlista.txt');

$ord = explode("\n", $ordlista);

sort($ord,SORT_STRING);

foreach ($ord as $key => $val) {
    echo $val."<br/>";
}
?>

Thanks ahead for answers!

提前感谢您的回答!

Solved:

解决了:

<?php
$ordlista = file_get_contents('C:/wamp/www/bilder/filmlista.txt');

$ord = explode("\n", $ordlista);

$ord=array_map(function($word) { return ucwords($word); }, $ord);


sort($ord,SORT_STRING);

foreach ($ord as $key => $val) {
    echo $val."<br/>";
}
?>

回答by iroel

$ord = array_map('ucfirst', $ord);

回答by icktoofay

$ord=array_map(function($word) { return ucfirst($word); }, $ord);

回答by Timo Huovinen

To support UTF-8 multibyte characters, like "Russian" for example, you would need

要支持 UTF-8 多字节字符,例如“俄语”,您需要

$ord = array_map(function($str){
    return mb_strtoupper(mb_substr($str, 0, 1)).mb_strtolower(mb_substr($str, 1));
}, $ord);

This uses the mb_ucfirstfunction from https://stackoverflow.com/a/14161325/175071

这使用mb_ucfirst来自https://stackoverflow.com/a/14161325/175071的函数