PHP capitalize after dash

后端 未结 9 1685
长情又很酷
长情又很酷 2021-01-17 13:46
$q = durham-region;

$q = ucfirst($q);

$q = Durham-region;

How would I capitalize the letter after the dash (Durham-Region)? Would I have to spli

9条回答
  •  一个人的身影
    2021-01-17 14:49

    Updated Solution

    As of PHP 5.5, the e modifier for preg_replace has been deprecated. The best option now is to use one of the suggestions that does not use this, such as:

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

    or

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

    Original Answer

    You could use preg_replace using the e modifier this way:

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

提交回复
热议问题