php - 对非英语字符使用 ucwords

标签 php regex

目前,我正在使用 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/

相关文章:

python - 模式内有模式重复的正则表达式

php - 有没有更高效的更新数据库值的方法?

php - 如何使用 join 子句仅检查行是否存在?

php - imagepng() 和 imagegif() 中的图像透明度

javascript - 正则表达式:如何替换以 "w/"和 "h"开头的四位数字?

javascript - 如何在 javascript 循环中使用正则表达式匹配组中的数字范围?

php - 在 php 中创建一个循环以在查询中工作

php - 为什么日期 "2012-12-01"导致打印的表格一遍又一遍地重复它?

php - 从文本中过滤坏词

jQuery 验证 HTML5 中的正则表达式