目前,我正在使用 ucwords 相关函数在连字符、点和撇号后制作大写字母:
function ucwordsMore ($str){
$str = ucwords($str);
$str = str_replace('- ','-',ucwords(str_replace('-','- ',$str))); // hyphens
$str = str_replace('. ','.',ucwords(str_replace('.','. ',$str))); // dots
$str = preg_replace("/\w[\w']*/e", "ucwords('\\0')", $str); // apostrophes
return $str;
}
对于英文字母来说效果很好。但是,无法正确识别非英文字母。例如这段文字:
La dernière usine française d'accordéons reste à Tulle
变成了这样的文字:
La DernièRe Usine FrançAise D'accordéOns Reste à Tulle
但我需要它是:
La Dernière Usine Française D'Accordéons Reste À Tulle
有什么想法吗?
最佳答案
正如@Jon提到的,您需要使用区域设置
来实现大小写之间的关系,从而影响使用它的函数调用。通常是LC_CTYPE
。
数字行为、排序、货币等也有常量。语言环境需要安装在您的计算机上,或者可以通过插件或模块等使用。请阅读相关内容。
我根本不知道 php 语言环境,所以这里是 Perl 中的一个示例,它使用与您不同的正则表达式方法。我无法很好地找出你的解决方案,希望你能从我的文章中得到一些想法。
use locale;
use POSIX qw(locale_h);
setlocale(LC_CTYPE, "en_US");
$str = "La dernière usine française d'accordéons reste à Tulle";
$str =~ s/ (?:^|(?<=\s)|(?<=\w-)|(?<=\w\.)|(?<=\w\')) (\w) / uc($1) /xeg;
print "$str\n";
输出
La Dernière Usine Française D'Accordéons Reste À Tulle
正则表达式
Form is s/// find and replace
s/ # Search
(?: # Group
^ # beginning of string
| (?<=\s) # or, lookbehind \s
| (?<=\w-) # or, lookbehind \w-
| (?<=\w\.) # or, lookbehind \w\.
| (?<=\w\') # or, lookbehind \w\'
) # End group
(\w) # Capture group 1, a single word char
/ # Replace
uc($1) # Upercased word char from capt grp 1
/xeg; # Modifiers x(expanded), e(eval), g(global)
关于php - 对非英语字符使用 ucwords,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9823703/