Jump to content

ucwords() exclusion (of/and/the, etc.)


HaLo2FrEeEk

Recommended Posts

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

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";

?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.