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
Make first every first character uppercase in array
提问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的函数

