简体   繁体   中英

how to get string first character and after (-) first character uppercase in php?

I want to change a string in uppercase. I am successfully doing it but I am able to do only first character and I want after(-) the first character also change.

$res = "mogli-story";
$strFinal = ucfirst($res);

Then the output is:

Mogli-story 

I want to get output like:

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

This should do the job:

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

This splits the string at each - character, uses ucfirst on each string in the resulting array, then joins them back together with - s.

Taking into account when there is no character

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","-");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM