在PHP中如何大写破折号后的第一个字母?[duplicate]

ykejflvf  于 2023-01-29  发布在  PHP
关注(0)|答案(9)|浏览(141)
    • 此问题在此处已有答案**:

Make all words lowercase and the first letter of each word uppercase(3个答案)
1年前关闭。
此帖子已于9天前编辑并提交审核,未能重新打开帖子:
原始关闭原因未解决

$q = 'durham-region';

$q = ucfirst($q);

$q = 'Durham-region';

破折号(Durham-Region)后面的字母应该怎么大写?我必须把两个分开并大写吗?

rjee0c15

rjee0c151#

更新的解决方案

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

$q = ucwords($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
0aydgbwb

0aydgbwb2#

多亏了ucwordsdelimiter参数,从PHP 5.4.32和5.5.16开始,它就像这样简单:

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

rbpvctlc3#

不使用e PCRE修饰符进行包络的一行程序:

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

kokeuurv4#

另一条线:

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

xkftehaa5#

是的。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;
}
cbjzeqam

cbjzeqam6#

您可以使用如下正则表达式回调方法来完成此操作:

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

ibrsph3r7#

需要注意的是,这里提供的解决方案不适用于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= Τάχιστη Αλώπηξ Βαφήσ Ψημένη Γη, Δρασκελίζει-Υπέρ Νωθρού Κυνόσ
dfddblmv

dfddblmv8#

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

UpperCaseAfterDash("input-text");

上述函数返回输入文本
如果你只需要在一个破折号后面加上大写字母,例如城市名称(Jastrzbie-Zdrój),这就足够了,但是如果你需要不止一个...,只需计算有多少数组元素(在上面的代码中分解后)存在,然后使用循环。
问候,

relj7zay

relj7zay9#

这里有一个通用函数,可以让你在任何字符或字符串后大写。在提问者所问的特定情况下,指针就是破折号。

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;

}

相关问题