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 Link to comment https://forums.phpfreaks.com/topic/115102-solved-ucfirst/ 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 Link to comment https://forums.phpfreaks.com/topic/115102-solved-ucfirst/#findComment-591930 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 :-) Link to comment https://forums.phpfreaks.com/topic/115102-solved-ucfirst/#findComment-591935 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 Link to comment https://forums.phpfreaks.com/topic/115102-solved-ucfirst/#findComment-591998 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 Link to comment https://forums.phpfreaks.com/topic/115102-solved-ucfirst/#findComment-592005 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 Link to comment https://forums.phpfreaks.com/topic/115102-solved-ucfirst/#findComment-592011 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 ?> Link to comment https://forums.phpfreaks.com/topic/115102-solved-ucfirst/#findComment-592014 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. Link to comment https://forums.phpfreaks.com/topic/115102-solved-ucfirst/#findComment-592021 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.