如何在php中获取字符串的第一个字符和(-)后的第一个字符的大写字母?

如何在php中获取字符串的第一个字符和(-)后的第一个字符的大写字母?,php,Php,我想更改大写字符串。我成功地做到了这一点,但我只能做第一个字符,我想在(-)之后,第一个字符也会改变 $res = "mogli-story"; $strFinal = ucfirst($res); 那么输出是: Mogli-story 我希望得到如下输出: Mogli-Story (First letter capital and the First letter after (-) capital) 这应该可以做到: $strFinal = implode("-", array_ma

我想更改大写字符串。我成功地做到了这一点,但我只能做第一个字符,我想在(-)之后,第一个字符也会改变

$res = "mogli-story";
$strFinal = ucfirst($res);
那么输出是:

Mogli-story 
我希望得到如下输出:

Mogli-Story (First letter capital and the First letter after (-) capital)

这应该可以做到:

$strFinal = implode("-", array_map(ucfirst, explode("-", $res)))

这将在每个
-
字符处拆分字符串,对结果数组中的每个字符串使用
ucfirst
,然后将它们与
-
s重新连接在一起。

这应该可以完成以下工作:

$strFinal = implode("-", array_map(ucfirst, explode("-", $res)))

这将在每个
-
字符处拆分字符串,在结果数组中的每个字符串上使用
ucfirst
,然后将它们与
-
s重新连接在一起。

考虑到没有字符的情况

function headcase($str,$sym){
    if (!empty($sym)){
        $str1 = substr($str, 0, strpos($str, $sym));
        $str2 = substr($str, strpos($str, $sym) + 1);  
        $final = ucfirst($str1).$sym.ucfirst($str2);
    }else{
        $final = ucfirst($str);

    }
    return $final;
}
echo headcase("mogli2","");
echo headcase("mogli-story","-");

考虑到没有字符时

function headcase($str,$sym){
    if (!empty($sym)){
        $str1 = substr($str, 0, strpos($str, $sym));
        $str2 = substr($str, strpos($str, $sym) + 1);  
        $final = ucfirst($str1).$sym.ucfirst($str2);
    }else{
        $final = ucfirst($str);

    }
    return $final;
}
echo headcase("mogli2","");
echo headcase("mogli-story","-");

谢谢你的回复。如果(-)不在字符串中怎么办?因为内容是动态的。它可以是story2而不是mogli Story,然后它应该大写第一个字母,我相信你会想要的。一定要在这样的值上测试它。谢谢你的回复。如果(-)不在字符串中怎么办?因为内容是动态的。它可以是story2而不是mogli Story,然后它应该大写第一个字母,我相信你会想要的。无论如何,在这样一个值上测试它。