user2435227
user2435227

Reputation: 21

php capitalize second letter in sentence min 10 characters long

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

Upvotes: 1

Views: 2543

Answers (2)

user3848478
user3848478

Reputation: 27

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

Upvotes: 2

M.B Kakadiya
M.B Kakadiya

Reputation: 576

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

Upvotes: 0

Related Questions