使用 preg_replace 将单词的首字母大写

发布于 2024-09-13 21:13:33 字数 203 浏览 5 评论 0原文

我需要将始终小写的名称变成大写。

例如约翰·约翰逊 -> John Johnsson

还有:

jonny-bart johnsson -> Jonny-Bart Johnsson

如何使用 PHP 完成此任务?

I need to turn names that are always in lower case into uppercase.

e.g. john johnsson -> John Johnsson

but also:

jonny-bart johnsson -> Jonny-Bart Johnsson

How do I accomplish this using PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

违心° 2024-09-20 21:13:33

您还可以使用正则表达式:

preg_replace_callback('/\b\p{Ll}/', 'callback', $str)

\b 表示单词边界,\p{Ll} 描述 Unicode 中的任何小写字母。 preg_replace_callback 将为每场比赛调用一个名为 callback 的函数,并且将匹配项替换为其返回值:

function callback($match) {
    return mb_strtoupper($match[0]);
}

这里mb_strtoupper用于将匹配到的小写字母转成小写为大写。

You could also use a regular expression:

preg_replace_callback('/\b\p{Ll}/', 'callback', $str)

\b represents a word boundary and \p{Ll} describes any lowercase letter in Unicode. preg_replace_callback will call a function called callback for each match and replace the match with its return value:

function callback($match) {
    return mb_strtoupper($match[0]);
}

Here mb_strtoupper is used to turn the matched lowercase letter to uppercase.

与风相奔跑 2024-09-20 21:13:33

如果您期望使用 unicode 字符...或者即使您不期望,我建议使用 mb_convert_case。当有 php 函数可以实现此目的时,您不需要使用 preg_replace 。

If you're expecting unicode characters...or even if you're not, I recommend using mb_convert_case nonetheless. You shouldn't need to use preg_replace when there's a php function for this.

浮光之海 2024-09-20 21:13:33
<?php
//FUNCTION

function ucname($string) {
    $string =ucwords(strtolower($string));

    foreach (array('-', '\'') as $delimiter) {
      if (strpos($string, $delimiter)!==false) {
        $string =implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
      }
    }
    return $string;
}
?>
<?php
//TEST

$names =array(
  'JEAN-LUC PICARD',
  'MILES O\'BRIEN',
  'WILLIAM RIKER',
  'geordi la forge',
  'bEvErly CRuSHeR'
);
foreach ($names as $name) { print ucname("{$name}\n"); }

//PRINTS:
/*
Jean-Luc Picard
Miles O'Brien
William Riker
Geordi La Forge
Beverly Crusher
*/
?>

来自 PHP 手册条目 ucwords 的评论

<?php
//FUNCTION

function ucname($string) {
    $string =ucwords(strtolower($string));

    foreach (array('-', '\'') as $delimiter) {
      if (strpos($string, $delimiter)!==false) {
        $string =implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
      }
    }
    return $string;
}
?>
<?php
//TEST

$names =array(
  'JEAN-LUC PICARD',
  'MILES O\'BRIEN',
  'WILLIAM RIKER',
  'geordi la forge',
  'bEvErly CRuSHeR'
);
foreach ($names as $name) { print ucname("{$name}\n"); }

//PRINTS:
/*
Jean-Luc Picard
Miles O'Brien
William Riker
Geordi La Forge
Beverly Crusher
*/
?>

From comments on the PHP manual entry for ucwords.

情绪 2024-09-20 21:13:33

使用正则表达式:

$out = preg_replace_callback("/[a-z]+/i",'ucfirst_match',$in);

function ucfirst_match($match)
{
    return ucfirst(strtolower($match[0]));
}

with regexps:

$out = preg_replace_callback("/[a-z]+/i",'ucfirst_match',$in);

function ucfirst_match($match)
{
    return ucfirst(strtolower($match[0]));
}
[旋木] 2024-09-20 21:13:33

这是我想出的(经过测试)...

$chars="'";//characters other than space and dash
           //after which letters should be capitalized

function callback($matches){
    return $matches[1].strtoupper($matches[2]);
}

$name="john doe";
$name=preg_replace_callback('/(^|[ \-'.$chars.'])([a-z])/',"callback",$name);

或者如果你有 php 5.3+,这可能会更好(未经测试):

function capitalizeName($name,$chars="'"){
    return preg_replace_callback('/(^|[ \-'.$chars.'])([a-z])/',
        function($matches){
            return $matches[1].strtoupper($matches[2]);
        },$name);
}

我的解决方案比其他发布的一些解决方案更冗长,但我相信它提供了最好的灵活性(您可以修改 $chars 字符串来更改可以分隔名称的字符)。

Here's what I came up with (tested)...

$chars="'";//characters other than space and dash
           //after which letters should be capitalized

function callback($matches){
    return $matches[1].strtoupper($matches[2]);
}

$name="john doe";
$name=preg_replace_callback('/(^|[ \-'.$chars.'])([a-z])/',"callback",$name);

Or if you have php 5.3+ this is probably better (untested):

function capitalizeName($name,$chars="'"){
    return preg_replace_callback('/(^|[ \-'.$chars.'])([a-z])/',
        function($matches){
            return $matches[1].strtoupper($matches[2]);
        },$name);
}

My solution is a bit more verbose than some of the others posted, but I believe it offers the best flexibility (you can modify the $chars string to change which characters can separate names).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文