【问题标题】:PHP capitalize after dash [duplicate]PHP在破折号后大写[重复]
【发布时间】:2011-04-05 01:18:34
【问题描述】:
$q = 'durham-region';

$q = ucfirst($q);

$q = 'Durham-region';

如何将破折号后的字母大写(达勒姆地区)?我是否必须将两者分开并大写?

【问题讨论】:

标签: php


【解决方案1】:

更新的解决方案

从 PHP 5.5 开始,preg_replacee 修饰符已被弃用。现在最好的选择是使用不使用这个的建议之一,例如:

$q = preg_replace_callback('/(\w+)/g', create_function('$m','return ucfirst($m[1]);'), $q)

$q = implode('-', array_map('ucfirst', explode('-', $q)));

原答案

您可以通过这种方式使用e 修饰符来使用preg_replace

$test = "durham-region";
$test = preg_replace("/(\w+)/e","ucfirst('\\1')", $test);
echo $test;
// Durham-Region

【讨论】:

  • 注意:使用 preg_replace_callback 可能会导致白屏,就像我的情况一样(意外的 T_FUNCTION)。感谢@Kelly 提供其他选项!
  • 当心,将 preg_replace 与包含变音符号的单词一起使用,例如$test = "wärme-test"; 导致类似“WäRme-Test”的结果。换句话说,元音变音之后的字母也会大写。
  • 注意,这不仅在破折号后大写,而且在每个非单词字符之后,甚至字符串的第一个字符都大写。
  • create_function 在 PHP 7.4 中也被弃用了
【解决方案2】:

感谢ucwordsdelimiter参数,从PHP 5.4.32和5.5.16开始,就这么简单:

$string = ucwords($string, "-");

【讨论】:

  • 这也行:$string = mb_convert_case($string, MB_CASE_TITLE, 'UTF-8');
【解决方案3】:

不使用e PCRE 修饰符的单线:

$str = implode('-', array_map('ucfirst', explode('-', $str)));

【讨论】:

  • 这个单线对我有用(也适用于包含元音变音的单词)。
【解决方案4】:

另一个单行:

str_replace(' ','',ucwords(str_replace('-',' ',$action)))

【讨论】:

  • 请注意,这可能会影响同时包含空格和破折号的字符串,但对于单个单词,这也很好。
【解决方案5】:

是的。 ucfirst() 只是将字符串的第一个字母大写。如果要将多个字母大写,则必须创建多个字符串。

$strings = explode("-", $string);
$newString = "";
foreach($strings as $string){
    $newString += ucfirst($string);
}

function ucfirst_all($delimiter, $string){
    $strings = explode("-", $string);
    $newString = "";
    foreach($strings as $string){
        $newString += ucfirst($string);
    }
    return $newString;
}

【讨论】:

    【解决方案6】:

    您可以使用这样的正则表达式回调方法:

    $q = preg_replace_callback('/\-([a-z]+)/g', create_function(
                '$m', 'return "-" . ucfirst($m[1]);'
            ),$q)
    

    【讨论】:

      【解决方案7】:

      请务必注意,此处提供的解决方案不适用于 UTF-8 字符串!

      $str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει-υπέρ νωθρού κυνός";
      $str = explode('-', mb_convert_case( $str, MB_CASE_TITLE ) );
      $str = implode('-', array_map('mb_convert_case', $str, array(MB_CASE_TITLE, "UTF-8")) );
      echo $str;
      
      // str= Τάχιστη Αλώπηξ Βαφήσ Ψημένη Γη, Δρασκελίζει-Υπέρ Νωθρού Κυνόσ
      

      【讨论】:

      • 我在你的第三行收到mb_convert_case() expects parameter 2 to be int, string given
      【解决方案8】:

      function UpperCaseAfterDash($wyraz)
        {
           $rozbij = explode('-',$wyraz);
           echo $rozbij[0].'-'.
           ucfirst($rozbij[1]);
        }
      
      UpperCaseAfterDash("input-text");
      

      以上函数返回input-Text

      如果您只需要一个破折号后的大写字母,例如城市名称 (Jastrzębie-Zdrój) 就足够了,但如果您需要多个...,只需计算数组元素的数量(在上述代码中分解后) ) 存在,然后使用循环。

      你好,

      【讨论】:

        【解决方案9】:

        这是一个通用函数,可让您在任何字符或字符串后大写。在提问者询问的具体情况下,针就是破折号。

        function capitalizeAfter( $needle, $haystack ) {
        
          $haystack = str_replace( $needle . "a", $needle . "A", $haystack );
          $haystack = str_replace( $needle . "b", $needle . "B", $haystack );
          $haystack = str_replace( $needle . "c", $needle . "C", $haystack );
          $haystack = str_replace( $needle . "d", $needle . "D", $haystack );
          $haystack = str_replace( $needle . "e", $needle . "E", $haystack );
          $haystack = str_replace( $needle . "f", $needle . "F", $haystack );
          $haystack = str_replace( $needle . "g", $needle . "G", $haystack );
          $haystack = str_replace( $needle . "h", $needle . "H", $haystack );
          $haystack = str_replace( $needle . "i", $needle . "I", $haystack );
          $haystack = str_replace( $needle . "j", $needle . "J", $haystack );
          $haystack = str_replace( $needle . "k", $needle . "K", $haystack );
          $haystack = str_replace( $needle . "l", $needle . "L", $haystack );
          $haystack = str_replace( $needle . "m", $needle . "M", $haystack );
          $haystack = str_replace( $needle . "n", $needle . "N", $haystack );
          $haystack = str_replace( $needle . "o", $needle . "O", $haystack );
          $haystack = str_replace( $needle . "p", $needle . "P", $haystack );
          $haystack = str_replace( $needle . "q", $needle . "Q", $haystack );
          $haystack = str_replace( $needle . "r", $needle . "R", $haystack );
          $haystack = str_replace( $needle . "s", $needle . "S", $haystack );
          $haystack = str_replace( $needle . "t", $needle . "T", $haystack );
          $haystack = str_replace( $needle . "u", $needle . "U", $haystack );
          $haystack = str_replace( $needle . "v", $needle . "V", $haystack );
          $haystack = str_replace( $needle . "w", $needle . "W", $haystack );
          $haystack = str_replace( $needle . "x", $needle . "X", $haystack );
          $haystack = str_replace( $needle . "y", $needle . "Y", $haystack );
          $haystack = str_replace( $needle . "z", $needle . "Z", $haystack );
        
          return $haystack;
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-25
          • 2012-08-07
          • 1970-01-01
          • 1970-01-01
          • 2015-05-01
          • 1970-01-01
          • 2023-03-20
          • 2016-04-19
          相关资源
          最近更新 更多