etrader Posted July 29, 2011 Share Posted July 29, 2011 I want to capitalized the first words in a sentence. This is fairly done by ucwords() but it will consider words after a space. I have a string with the form of $str="word1-word2_word3"; how I capitize it to $str="Word1-Word2_Word3"; ? Link to comment https://forums.phpfreaks.com/topic/243202-ucwords-for-a-string-without-space/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 29, 2011 Share Posted July 29, 2011 I would use preg_replace with the 'e' modifier to get it to call ucfirst for each word found. Link to comment https://forums.phpfreaks.com/topic/243202-ucwords-for-a-string-without-space/#findComment-1249085 Share on other sites More sharing options...
PFMaBiSmAd Posted July 29, 2011 Share Posted July 29, 2011 Specifically - <?php $str="word1-word2_word3 word4"; $str = preg_replace("/([^\W_]+)/e", "ucfirst('\\1')", $str); echo $str; ?> Link to comment https://forums.phpfreaks.com/topic/243202-ucwords-for-a-string-without-space/#findComment-1249088 Share on other sites More sharing options...
etrader Posted July 29, 2011 Author Share Posted July 29, 2011 Thanks for you handy idea Link to comment https://forums.phpfreaks.com/topic/243202-ucwords-for-a-string-without-space/#findComment-1249116 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.