russthebarber Posted November 10, 2010 Share Posted November 10, 2010 Hi, I have set up a simple function so that when a user enters a title in a php form, php gives the first letter of each word a capital letter. As follows: function caps($text){ $search_text=$text; $search_text=ucwords(strtolower($search_text)); $look_for = "(a"; $change_to = "(A"; $changed_text = str_replace($look_for, $change_to, $search_text); $search_text=$changed_text; $look_for = "(b"; $change_to = "(B"; $changed_text = str_replace($look_for, $change_to, $search_text); $search_text=$changed_text; $look_for = "(c"; $change_to = "(C"; //...etc...etc.. up to $look_for = "(z"; $change_to = "(Z"; $changed_text = str_replace($look_for, $change_to, $search_text); $search_text=$changed_text; return $search_text; } The trouble is, if I were to enter the following "The secret of DNA", my function would return "The Secret Of Dna" (removes the caps). Any ideas how to get around this would be most useful. Thanks in advance. Russ Quote Link to comment https://forums.phpfreaks.com/topic/218288-escaping-caps/ Share on other sites More sharing options...
trq Posted November 10, 2010 Share Posted November 10, 2010 ucfirst. Quote Link to comment https://forums.phpfreaks.com/topic/218288-escaping-caps/#findComment-1132604 Share on other sites More sharing options...
russthebarber Posted November 10, 2010 Author Share Posted November 10, 2010 On looking up ucfirst() it seems that that only capitalises the first word in the string, not the first letter of every word....but in looking it up I found that I just need ucwords() on its own. So, thanks for the help. Problem solved! Quote Link to comment https://forums.phpfreaks.com/topic/218288-escaping-caps/#findComment-1132617 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.