【问题标题】:Add spaces between words in a camelCased string then uppercase the first word在驼峰式字符串中的单词之间添加空格,然后将第一个单词大写
【发布时间】:2011-03-25 20:38:48
【问题描述】:

我有两种类型的字符串,hellohelloThere

我想要改变它们,使它们看起来像:HelloHello There,视情况而定。

这样做的好方法是什么?

【问题讨论】:

  • 我想你有一本字典来确定复合词在哪里分裂?!

标签: php string uppercase lowercase title-case


【解决方案1】:

将 CamelCase 转换为不同的单词:

preg_replace('/([^A-Z])([A-Z])/', "$1 $2", $string)

将所有单词的首字母大写:

ucwords()

所以在一起:

ucwords(preg_replace('/([^A-Z])([A-Z])/', "$1 $2", $string))

【讨论】:

    【解决方案2】:

    使用ucwords函数:

    返回第一个字符串 str 中每个单词的字符 大写,如果那个字符是 字母。

    单词的定义是任意字符串 立即出现的字符 在空格之后(这些是:空格, 换页、换行、回车、 水平制表符和垂直制表符)。

    这不会拆分拼在一起的单词 - 您必须根据需要在字符串中添加空格才能使此功能正常工作。

    【讨论】:

    • 他需要先用helloThere添加空格
    【解决方案3】:

    使用ucwords函数:

    echo ucwords('hello world');
    

    【讨论】:

      【解决方案4】:

      你可以像大家说的那样使用ucwords...在helloThere中添加空格你可以使用$with_space = preg_replace('/[A-Z]/'," $0",$string);然后ucwords($with_space);

      【讨论】:

        【解决方案5】:

        PHP 有许多字符串操作函数。 ucfirst() 会为你做的。

        http://ca3.php.net/manual/en/function.ucfirst.php

        【讨论】:

        • 不,该功能不能将 CamelCase 更改为 Camel Case
        【解决方案6】:

        使用 ucwords

        <?php
        $foo = 'hello world';
        $foo = ucwords($foo);             // Hello world
        
        $bar = 'BONJOUR TOUT LE MONDE!';
        $bar = ucwords($bar);             // HELLO WORLD
        $bar = ucwords(strtolower($bar)); // Hello World
        ?>
        

        【讨论】:

          【解决方案7】:

          为了让舒尔在其他语言上也能工作,UTF-8 可能是一个好主意。我在我的 wordpress 安装中为任何语言使用这个防水。

          $str = mb_ucfirst($str, 'UTF-8', true);
          

          这使首字母大写,所有其他小写。如果第三个 arg 设置为 false(默认值),则不会操纵字符串的其余部分。但是,这里有人可能会建议一个参数来重用函数本身,并在第一个单词之后将每个单词 mb 大写,以更准确地回答这个问题。

          // Extends PHP
          if (!function_exists('mb_ucfirst')) {
          
          function mb_ucfirst($str, $encoding = "UTF-8", $lower_str_end = false) {
              $first_letter = mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding);
              $str_end = "";
              if ($lower_str_end) {
                  $str_end = mb_strtolower(mb_substr($str, 1, mb_strlen($str, $encoding), $encoding), $encoding);
              } else {
                  $str_end = mb_substr($str, 1, mb_strlen($str, $encoding), $encoding);
              }
              $str = $first_letter . $str_end;
              return $str;
          }
          
          }
          

          /伦德曼

          【讨论】:

            【解决方案8】:

            您无需捕获任何字母即可在单词之间注入空格——前瞻就可以了。然后在添加空格后应用多字节安全的标题大小写函数。

            代码:(Demo)

            echo mb_convert_case(
                     preg_replace('~(?=\p{Lu})~u', ' ','helloThere'),
                     MB_CASE_TITLE
                 );
            // Hello There
            

            【讨论】:

              猜你喜欢
              • 2022-11-02
              • 2011-08-14
              • 1970-01-01
              • 2011-04-28
              • 1970-01-01
              • 1970-01-01
              • 2017-09-11
              • 2018-03-17
              相关资源
              最近更新 更多