Connecting Tech Pros Worldwide Forums | Help | Site Map

translate each word in string to uppercase with exceptions

Member
 
Join Date: Jan 2008
Posts: 121
#1: Mar 24 '08
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

TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#2: Mar 24 '08

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!
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#3: Mar 24 '08

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!
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#4: Mar 25 '08

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
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#5: Mar 25 '08

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.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#6: Mar 25 '08

re: translate each word in string to uppercase with exceptions


Challenge during eating and movie watching ;-)

Ronald
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#7: Mar 25 '08

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!
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#8: Mar 25 '08

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
#9: Mar 25 '08

re: translate each word in string to uppercase with exceptions


Thanks for your replies everyone :)
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#10: Mar 25 '08

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
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#11: Mar 25 '08

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.
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#12: Mar 25 '08

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 ;)
Reply