translate each word in string to uppercase with exceptions | Member | | Join Date: Jan 2008
Posts: 121
| | |
Hi,
What i want to do is make the first letter of each word in a sting uppercase except "and" & "in"
How would i go about doing this?
Cheers,
Adam
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | | re: translate each word in string to uppercase with exceptions
ucwords — Uppercase the first character of each word in a string
$new_phrase = ucwords ( $phrase );
Then you need to find all the and's and what ever else you don't want changed and changed them back:
$final_phrase = str_replace ( ' And ' , ' and ' , $new_phrase );
Haven't tried it, but you see my logic!
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,949
| | | re: translate each word in string to uppercase with exceptions
~Here's something i quickly threw together, it's lengthy, and i'm sure someone can do it better, but here's my shot at it:
[php]
$_string = "going to make this uppercase but miss and & in"; # string to capitalise.
$_string = explode(" ", $_string); # explode the string into an array
$_ignore = array("and", "in"); # ignore list
foreach($_string as $_key => $_val) # traverse the array
{
if(in_array($_val, $_ignore)) # this $_val is in the ignore list
{
$_string[$_key] = $_val; # so
}
else # this $_val isn't in the ignore list
{
$_string[$_key] = ucfirst($_val); # so, using the $_key of the array, insert into relevant key.
}
}
$_string = implode(" ", $_string); # Rebuild the string from remodeled array
echo $_string;
[/php]
Hope it helps.
Regards!
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: translate each word in string to uppercase with exceptions
Only problem is that, when the 'to be excluded word' is, in the text, followed by a reader sign like dot, comma, question or exclamation mark, etc, it is translated to upper case. SO it will need some adaptation to accomodate that.
Ronald
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,949
| | | re: translate each word in string to uppercase with exceptions Quote:
Originally Posted by ronverdonk Only problem is that, when the 'to be excluded word' is, in the text, followed by a reader sign like dot, comma, question or exclamation mark, etc, it is translated to upper case. SO it will need some adaptation to accomodate that.
Ronald Ahhhh, touche!
I shall have a look at what i can do to acomodate this challenge!
Need to eat and watch a film first :)
Regards.
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: translate each word in string to uppercase with exceptions
Challenge during eating and movie watching ;-)
Ronald
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | | re: translate each word in string to uppercase with exceptions
Curious... What was wrong with my approach? I know it was simple, but I am here to learn!
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: translate each word in string to uppercase with exceptions Quote:
Originally Posted by TheServant Curious... What was wrong with my approach? I know it was simple, but I am here to learn! It appears to me that your solution works best, also regarding the special reading marks. You did very well!
Ronald
| | Member | | Join Date: Jan 2008
Posts: 121
| | | re: translate each word in string to uppercase with exceptions
Thanks for your replies everyone :)
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: translate each word in string to uppercase with exceptions
We are here to help: you even have two answers to choose from.
See you netx time.
Ronald
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,949
| | | re: translate each word in string to uppercase with exceptions Quote:
Originally Posted by TheServant Curious... What was wrong with my approach? I know it was simple, but I am here to learn! I hadn't even realised you posted before me!
Sorry.
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | | re: translate each word in string to uppercase with exceptions Quote:
Originally Posted by ronverdonk It appears to me that your solution works best, also regarding the special reading marks. You did very well!
Ronald Thanks Ronald :D Quote:
Originally Posted by markusn00b I hadn't even realised you posted before me!
Sorry. No problem ;)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|