RobinTibbs Posted February 3, 2007 Share Posted February 3, 2007 ok basically this function is meant to capitalise text properly, so given "HELLO THERE" it should return "Hello There". however what is being returned is "hello there". what am i missing? many thanks. code is probably big mess ^^ function prettyfyText($text){ $text = strtolower($text); $explode = explode(" ", $text); $i = 0; while($i < count($explode)) { $string = str_split($explode[$i]); $idx = 0; while($idx<count($string)){ if($idx == 0) { $string[$idx] = strtoupper($string[$idx]); } $idx++; //echo $string[0]." ".$string[1]."<br />"; $nice = implode("", $string); } $blah = implode(" ", $explode); $i++; } echo $blah; } // end function prettyfyText Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 3, 2007 Share Posted February 3, 2007 http://us3.php.net/ucwords Quote Link to comment Share on other sites More sharing options...
KrisNz Posted February 3, 2007 Share Posted February 3, 2007 If you're doing this for text your going to output on an html page, then its much easier to do this... <p style="text-transform:capitalize;">the fish was delish and it made quite a dish</p> Quote Link to comment Share on other sites More sharing options...
paul2463 Posted February 3, 2007 Share Posted February 3, 2007 Robin, obviously Jesirose's answer is the best way to do it as someone has very nicely written the function for you, but if you wanted a better way than your function does it you could have done it this way <?php function prettyfyText($text){ $text = strtolower($text); $explode = explode(" ", $text); $i = 0; $return = ""; //initialise the return sentence while($i < count($explode)) { $end = substr($explode[$i],1); //get all the letters after the first one $begin = strtoupper(substr($explode[$i],0,1)); // turn to uppercase the first letter of the word $newstr = "$begin$end"; //rebuild the word $return .= " $newstr"; //concatenate the sentence again $i++; //increase the counter } return $return; //return the rebuilt sentence echo prettyfyText("HELLO WORLD HOW ARE YOU"); // Hello World How Are You just because I had done it, not having myself seen the ucwords function before - so thanks from me too Jesi. Quote Link to comment Share on other sites More sharing options...
RobinTibbs Posted February 3, 2007 Author Share Posted February 3, 2007 thanks to you all, going to go with the ucwords but i've also noted down the other solutions just for reference. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 3, 2007 Share Posted February 3, 2007 Yep, always good to check the manual before trying to write stuff like this. It's often already been done A good way to check is to search the manual for similar functions. In this case, you already knew about strtoupper - at the bottom it says "See also strtolower(), ucfirst(), ucwords() and mb_strtoupper()." It's an easy way to learn about new functions, I find great shortcuts that way all the time. 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.