Capitalize a letter of a string

Shaffer 0 Tallied Votes 146 Views Share

This function caps a specific letter of a string, and returns the updated string.
THe first letter is 0, as it is default as well.

<?php
function cap_letter($string,$letter=0) {
  $letters = str_split($string);
  $letters[$letter] = strtoupper($letters[$letter]);
  return implode($letters);
}
?>
ShawnCplus 456 Code Monkey Team Colleague

I think you mean capitalize. If you capitolize it you turn it into money. I wasn't aware PHP was the philosopher's stone of the 21st century

Dani 4,310 The Queen of DaniWeb Administrator Featured Poster Premium Member

Title edited.

Dani 4,310 The Queen of DaniWeb Administrator Featured Poster Premium Member

Since a string is defined as an array of characters, you can actually just have line #4 without converting the string to and from an array. In other words:

$word = 'happy';
echo $word[0];

would return 'h'.

Shaffer 0 Newbie Poster

Hello,
Thanks for the feedback.
As for cscgal's comment, I want to say that I wanted to leave it with the least amount of code.

Thanks,
Shaffer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.