cordoprod Posted July 16, 2008 Share Posted July 16, 2008 Hello. I need some help with capitalizing the first letter of a string.. I use ucfirst() which works well if i have a string in lower captions.. But, i have upper captions and then ucfirst() doesn't work.. E.g: STRING - STRING string - String Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 16, 2008 Share Posted July 16, 2008 For ucfirst to work the string needs to be in lowercase, so use the strtolower() function first. Ken Quote Link to comment Share on other sites More sharing options...
cordoprod Posted July 16, 2008 Author Share Posted July 16, 2008 For ucfirst to work the string needs to be in lowercase, so use the strtolower() function first. Ken Thanks mate :-) Quote Link to comment Share on other sites More sharing options...
cordoprod Posted July 16, 2008 Author Share Posted July 16, 2008 For ucfirst to work the string needs to be in lowercase, so use the strtolower() function first. Ken Thanks mate :-) One more thing... If the last letter is the same as the first one it also get capitalized... E.g Example - ExamplE Quote Link to comment Share on other sites More sharing options...
spectacularstuff Posted July 16, 2008 Share Posted July 16, 2008 Use substr to only count the first letter and ignore the rest. $rest = substr("abcdef", 1); // returns "a" http://www.php.net/manual/en/function.substr.php Wayne Quote Link to comment Share on other sites More sharing options...
Fidsah Posted July 16, 2008 Share Posted July 16, 2008 Using this: $string = "EXAMPLE"; print ucfirst(strtolower($string)); gives me this: Example Quote Link to comment Share on other sites More sharing options...
xtopolis Posted July 16, 2008 Share Posted July 16, 2008 <?php $word = 'ExaMPle'; $word = ucfirst(strtolower($word)); $last = substr($word,strlen($word)-1); $word = substr_replace($word,strtoupper($last),strlen($word)-1,strlen($word)); //outputs ExamplE ?> Quote Link to comment Share on other sites More sharing options...
AndyB Posted July 16, 2008 Share Posted July 16, 2008 Although it may not be needed in this code requirement (although there might still be 'one more thing'), you might want to use the trim() function to remove any leading/trailing spaces that interfere with counting the number of characters, etc. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.