【问题标题】:PHP returning a string with the first char of every words in lowercasePHP返回一个字符串,其中每个单词的第一个字符都是小写的
【发布时间】:2013-07-10 21:52:51
【问题描述】:

PHP 中已经有一个名为 ucwords 的函数,它的作用与我需要的正好相反。

有没有这样的名为lcwords的php库?其中不是将每个单词的第一个大写,而是将它们转换为小写。

谢谢。

【问题讨论】:

标签: php lowercase


【解决方案1】:

这是一个单行:

implode(' ', array_map(function($e) { return lcfirst($e); }, explode(' ', $words)))

示例

function lcwords($words) {
  return implode(' ', array_map(function($e) { return lcfirst($e); }, explode(' ', $words)));
}

$words = "First Second Third";
$lowercased_words = lcwords($words);
echo($lowercased_words);

【讨论】:

    【解决方案2】:
    $string = "THIS IS SOME TEXT";
    $string=explode(" ",$string);
    $i=0;
    while($i<count($string)){
        $string[$i] = lcfirst($string[$i]);
        $i++;
    }
    echo implode(" ",$string);
    

    this link 找到另一个函数。

    【讨论】:

    • 小写。此外,在回答之前实际阅读问题并没有什么坏处。
    • 是的,我太快了。编辑了答案。
    • 感谢您的回答真的很有帮助。
    【解决方案3】:

    这可能对你有帮助

    $str="hello";
    $test=substr($str, 0,1);
    $test2=substr($str, 1,strlen($str));
    echo $test.strtoupper($test2);
    

    【讨论】:

    • 这根本不起作用,您只需获取字符串的第一个字符,然后将其余字符粘贴到它上面。
    【解决方案4】:

    我不得不尝试使用正则表达式:

    <?php
    $pattern = "/(\b[a-zA-Z])(\w)/";
    $string = "ThIs is A StriNG x y z!";
    
    // This seems to work 
    $result = preg_replace_callback($pattern, function($matches) {
        return (strtolower($matches[1]).$matches[2]);
    }, $string);
    
    echo $result;
    echo "\r\n";
    
    //This also seems to do the trick. Note that mb_ doesn't use / 
    echo mb_ereg_replace('(\b[a-zA-Z])(\w{0,})', "strtolower('\\1') . '\\2'", $string, 'e');
    
    // I wanted this to work but it didn't produce the expected result:
    echo preg_replace($pattern, strtolower("\$1") . "\$2", $string);
    echo "\r\n";
    

    【讨论】:

      【解决方案5】:

      更短的单行:

      implode(' ',array_map('lcfirst',explode(' ',$text)))
      

      【讨论】:

        【解决方案6】:
        function lcwords(string $str) :string
        {
            return preg_replace_callback('/(?<=^|\s)\w/', function (array $match) :string {
                return strtolower($match[0]);
            }, $str);
        }
        

        【讨论】:

          【解决方案7】:

          我知道这个话题很古老,但问题仍然存在,这很遗憾(即使现在也没有lcwords())。

          这里是lcwords(),用于将每个单词的每个首字母小写。

          简单地说:这个解决方案是通用的,任何标点符号都不应该是它的问题。当然,你要为此付出代价:)

          
              /**
               * Lowercase the first character of each word in a string
               *
               * @param  string $string     The input string.
               * @param string $delimiters The optional delimiters contains the word separator characters (regular expression)
               *
               * @return string             Returns the modified string.
               */
              function lcwords($string, $delimiters = "\W_\t\r\n\f\v") {
                  $string = preg_replace_callback("/([$delimiters])(\w)/", function($match) {
                      return $match[1].lcfirst($match[2]);
                  }, $string);
          
                  return lcfirst($string); // Uppercase first char if it's the beginning of the line
              }
          
              // Here is a couple of result examples:
          
              echo lcwords("/SuperModule/ActionStyle/Controller.php").PHP_EOL;
              // result:    /superModule/actionStyle/controller.php
          
              echo lcwords("SEPARATED\tBY TABS\nAND\rSPACES").PHP_EOL;
              // result:    sEPARATED  bY tABS  aND  sPACES
          
              echo lcwords("HELLO").PHP_EOL;
              // result:    hELLO
          
              echo lcwords("HEELO HOW-ARE_YOU").PHP_EOL;
              // result:    hEELO hOW-aRE_yOU
          
              echo lcwords("SEPARATED\tBY TABS\nAND\rSPACES").PHP_EOL;
              // result:    sEPARATED  bY tABS  aND  sPACES
          

          /([$delimiters])(\w)/ - 模式有两组:搜索 1 个非单词字符,后跟 1 个单词字符。然后单词字符将在回调中大写。所以只有选定的字符集得到更新 - 内容的最小变化。

          【讨论】:

            【解决方案8】:

            “小写字符串 php 中每个单词的第一个字符”,这是您得到的第一个响应:http://php.net/manual/en/function.lcfirst.php

            【讨论】:

            • 我这样做了,但在示例中清楚地表明它只小写给定字符串中第一个单词的第一个字符,无论该字符串中有多少单词。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-11-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多