php capitalize second letter in sentence min 10 characters long

为君一笑 提交于 2019-12-24 13:55:01

问题


Hello I have a project where I need to cApitalize only the second letter in a sentence. I now that PHP has strtoupper() and string strtoupper ( string $string ) ucfirst() returns first letter

So here is my best attempt

<?php

$str = "capitalize";

$str = ucfirst(strtolower($str)); // makes all the letters lower case 
?>

This is where I get confused if 0 = the first letter and 1= 2nd then could I just make an array(") or count_chars() then $val


回答1:


Its a old question, just came across this so would put an answer based on @doppelgreener comment.

This should work :

$str = "capitalize";
$str[1]= strtoupper($str[1]);
echo $str; // cApitalize



回答2:


i have one idea to perform this operation.. example

$strmain='capitalize';
$result = substr($strmain, 0, 1); //result is c
$result1=str_replace($result,'',$strmain);//now your result1 is apitalize
$result2=ucfirst($result1); //now result2 is Apitalize

$finalresult=$result.$result2 ///now your finalresult is cApitalize


来源:https://stackoverflow.com/questions/16829362/php-capitalize-second-letter-in-sentence-min-10-characters-long

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!