cyborgeek Posted May 30, 2013 Share Posted May 30, 2013 <?php $str = "capitalize "; $str_implode = str_split($str); $caps = true; foreach($str_implode as $key=>$letter){ if($caps){ $out = strtoupper($letter); if($out <> " ") //not a space character $caps = false; echo $str; ?> // I realize this is an error unsure if I need to array() or convert case ?? Link to comment https://forums.phpfreaks.com/topic/278583-trying-to-capitalize-only-the-second-letter-in-a-sentence/ Share on other sites More sharing options...
PravinS Posted May 30, 2013 Share Posted May 30, 2013 Try this $result = str_replace(substr($word,1,1),ucfirst(substr($word,1,1)),$word); Link to comment https://forums.phpfreaks.com/topic/278583-trying-to-capitalize-only-the-second-letter-in-a-sentence/#findComment-1433134 Share on other sites More sharing options...
Eiseth Posted May 30, 2013 Share Posted May 30, 2013 Use for..loop to loop through each character $string = 'capitalize'; $new = ''; for ($i = 0, $count = strlen($string); $i < $count; $i++) { if ($i == 1) { $new .= strtoupper($string[$i]); } else { $new .= $string[$i]; } } echo $new; Link to comment https://forums.phpfreaks.com/topic/278583-trying-to-capitalize-only-the-second-letter-in-a-sentence/#findComment-1433136 Share on other sites More sharing options...
requinix Posted May 30, 2013 Share Posted May 30, 2013 str_replace() is wasteful and will only work if you pass a $count=1 argument. A loop to copy the string is even worse. $string = "capitalize"; $string[1] = strtoupper($string[1]); Link to comment https://forums.phpfreaks.com/topic/278583-trying-to-capitalize-only-the-second-letter-in-a-sentence/#findComment-1433247 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.