473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

translate each word in string to uppercase with exceptions

133 100+
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
Mar 24 '08 #1
11 1868
TheServant
1,168 Expert 1GB
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!
Mar 24 '08 #2
Markus
6,050 Expert 4TB
~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!
Mar 24 '08 #3
ronverdonk
4,258 Expert 4TB
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
Mar 24 '08 #4
Markus
6,050 Expert 4TB
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.
Mar 24 '08 #5
ronverdonk
4,258 Expert 4TB
Challenge during eating and movie watching ;-)

Ronald
Mar 24 '08 #6
TheServant
1,168 Expert 1GB
Curious... What was wrong with my approach? I know it was simple, but I am here to learn!
Mar 25 '08 #7
ronverdonk
4,258 Expert 4TB
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
Mar 25 '08 #8
adamjblakey
133 100+
Thanks for your replies everyone :)
Mar 25 '08 #9
ronverdonk
4,258 Expert 4TB
We are here to help: you even have two answers to choose from.
See you netx time.

Ronald
Mar 25 '08 #10
Markus
6,050 Expert 4TB
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.
Mar 25 '08 #11
TheServant
1,168 Expert 1GB
It appears to me that your solution works best, also regarding the special reading marks. You did very well!

Ronald
Thanks Ronald :D

I hadn't even realised you posted before me!

Sorry.
No problem ;)
Mar 25 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Bart Nessux | last post by:
number = random.sample(range(count), 1) This makes 'number' into a one entry list (I don't know why as 'count' is an integer). Is there an easy way to convert 'number' back to an int? TIA
13
by: Nige | last post by:
To save me re-inventing the wheel, does anyone have a function to capitalise each word in form data? I was thinking along the lines of capitalise the first letter of the string, then any other...
97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
8
by: Darryl Kerkeslager | last post by:
I hope that although this is 25% Access and 75% Word, that someone will know ... The whole problem here arises because 1) Microsoft acknowledges an 'issue' wherein TextInput type FormFields are...
5
by: JrMc | last post by:
I am new to this .net environement. I'm needing to do a simple task - I need to read the first character of a word and replace it with an uppercase. Example: "horse" --> change to "Horse" ...
6
by: bobueland | last post by:
The module string has a function called translate. I tried to find the source code for that function. In: C:\Python24\Lib there is one file called string.py I open it and it says
10
by: ahoway | last post by:
I am having problems entering a sentence for translating into pig latin. It is set up now to read the entire sentence as one word. I would like to know how to look at each word in the sentence so...
6
by: 182719 | last post by:
<?php $testcase = 'AKLWC139'; if (ctype_upper($testcase)) { echo "The string $testcase consists of all uppercase letters. \n"; } else { echo "The string $testcase does not consist of all...
3
by: Kenneth McDonald | last post by:
I have the need to occasionally translate a single word programatically. Would anyone have a Python script that would let me do this using Google (or another) translation service? Thanks, Ken
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.