HaLo2FrEeEk Posted March 6, 2009 Share Posted March 6, 2009 I'd like to exclude certain words when using ucwords() in a string. For example, I might have a string that contains the word "of" or "the" and I don't want those capitalized. How would I do that? It needs to be a simple as possible, preferably without having to explode the string into an array. Is there a simple function to achiee what I'm looking for? Thanks! Link to comment https://forums.phpfreaks.com/topic/148299-ucwords-exclusion-ofandthe-etc/ Share on other sites More sharing options...
redarrow Posted March 6, 2009 Share Posted March 6, 2009 manual way. <?php echo "hello\n ".ucwords('redarrow')." how are you\n ".ucwords('mate!')." "; ?> Link to comment https://forums.phpfreaks.com/topic/148299-ucwords-exclusion-ofandthe-etc/#findComment-778564 Share on other sites More sharing options...
trq Posted March 6, 2009 Share Posted March 6, 2009 You'll need an array of the words you don't want captitalized. You then need to turn your string into an array of words and loop through it checking against your other array, if the current word isn't present capitalize it. Link to comment https://forums.phpfreaks.com/topic/148299-ucwords-exclusion-ofandthe-etc/#findComment-778577 Share on other sites More sharing options...
redarrow Posted March 6, 2009 Share Posted March 6, 2009 Or this way, i tried it and seems ok. still a manual way, Do what thorpe says! <?php $word="hello redarrow how are you mate!"; $test_array=explode(' ',$word); print_r($test_array); $test_array[1]=ucwords($test_array[1]); $test_array[5]=ucwords($test_array[5]); $words=implode(' ',$test_array); echo "$words"; ?> Link to comment https://forums.phpfreaks.com/topic/148299-ucwords-exclusion-ofandthe-etc/#findComment-778582 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 7, 2009 Author Share Posted March 7, 2009 I want to stay away from manually choosing words to capitilize, because the string will change. Thorpe, like this: $nocap = array("of", "the", "a", "or", etc...); $str = "some string that contains a few of the words in that list"; $str_arr = explode(" ", $str); foreach($str_arr as $word) { $string .= (!in_array($word)) ? ucfirst($word) : $word; echo " "; } echo trim($string); Something like that? It seems an awful lot to just have certain words not capitilized. You would think PHP wouldhave a built in function, or the ability to pass another arguement to ucwords to exclude certain words. Lame. Link to comment https://forums.phpfreaks.com/topic/148299-ucwords-exclusion-ofandthe-etc/#findComment-778649 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.