/    Sign up×
Community /Pin to ProfileBookmark

upper/lower case text

I have a function that takes a string and convert’s the words to proper case (upper/lower case). For some reason, word’s that have an ‘ in them, it capitalizes the next letter after the ‘. Could someone help me fix this? The code is below:

[code]
if ($case_magic == ‘y’) {
// now capitalize all first-letters of words, getting around php’s
// inability to recognized non-word delimiters
$title = preg_replace (“/(W?)(w+)(W?)/e”,
“‘\1’.ucwords(strtolower(‘\2’)).’\3′”, $title);

// these are the words that we don’t want first-letter-capitalized…
$words=array( ” a “,
” and “,
” the “,
” for “,
” to ” ,
” an ” ,
” on “);
foreach ($words as $w) {
$title=str_replace( ucwords(strtolower($w)), strtolower($w),$title);
}
}
[/code]

Thanks.

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@PineSolPirateJan 04.2007 — How about tossing some brackets and the ' char onto your regex?
$title = preg_replace ("/(W?)(['w]+)(W?)/e", "'\1'.ucwords(strtolower('\2')).'\3'", $title);
Copy linkTweet thisAlerts:
@jrthor2authorJan 04.2007 — that worked, but what if I have a name lik O'Donnell, then the D gets lowercased ? .
Copy linkTweet thisAlerts:
@PineSolPirateJan 04.2007 — Picky picky ?

I'll have to think about that...
Copy linkTweet thisAlerts:
@NightShift58Jan 05.2007 — I don't know if it's the ultimate solution but what if you modified your RegEx so as NOT to capitalize when 2 or fewer characters follow a single quote - that is, 2 or less between single quote and next word delimiter,

That way you could exclude [b]'m[/b] (as in I'm), [b]'s[/b] (as in he's), [b]'t[/b] (as in don't), [b]'re[/b] (as in we're), [b]'ve[/b] (as in we've),[b]'ll[/b] (as in they'll) and at the same time include most - if not all - of the Irish.

I can't think of any word with more than 2 characters following an apostrophe.
Copy linkTweet thisAlerts:
@aussie_girlJan 05.2007 — that worked, but what if I have a name lik O'Donnell, then the D gets lowercased ? .[/QUOTE]

Put a space after the ' e.g O' Donnell
Copy linkTweet thisAlerts:
@NightShift58Jan 05.2007 — Put a space after the ' e.g O' Donnell[/QUOTE]How does your RegEx know that it's "O'Donnell"?

And, if your RegEx knows that it's "O'Donnell", why bother putting the space - why not just capitalize the "D"?
Copy linkTweet thisAlerts:
@PineSolPirateJan 05.2007 — I think Aussie just means when you do the entry of the info. If you enter it as O' Donnel it would cap it right. But then if you had that much control on the enty you could just cap it right to begin with I think. ?
Copy linkTweet thisAlerts:
@NightShift58Jan 05.2007 — Yes, that was my drift...
Copy linkTweet thisAlerts:
@PineSolPirateJan 05.2007 — I think I did it with a second regex.
$title = preg_replace ("/(W?)(['w]+)(W?)/e", "'\1'.ucwords(strtolower('\2')).'\3'", $title);
$title = preg_replace ("/([A-Z]')(w)(w+)/e","'\1'.strtoupper('\2').'\3'", $title);


When I run it I get the following:

[B]Original:[/B]ThIs IS pOOrlY cAPitALIzed Mr. O'DOnnEL. IT'S THAT'S WE'RE.

[B]Final:[/B]This Is Poorly Capitalized Mr. O'Donnel. It's That's We're.

Could use more in depth testing though ?
Copy linkTweet thisAlerts:
@PineSolPirateJan 05.2007 — Also you might want to add 'is' to your exclusion array. Or you might not, just thought you may have missed it.

Also, just out of curiosity, what's this for? Book or movie titles or something? Just wondering ?
Copy linkTweet thisAlerts:
@aussie_girlJan 06.2007 — I think Aussie just means when you do the entry of the info. If you enter it as O' Donnel it would cap it right. But then if you had that much control on the enty you could just cap it right to begin with I think. ?[/QUOTE]
Yes that's what I meant, but what about names like McDonald how are you going to handle that?
Copy linkTweet thisAlerts:
@NightShift58Jan 06.2007 — I'm more of a KFC fan - but no luck there either...
Copy linkTweet thisAlerts:
@bathurst_guyJan 06.2007 — What is this going to be used for? Is it an assignment?
Copy linkTweet thisAlerts:
@PineSolPirateJan 06.2007 — Well, I've heard about this rather complex system called "Someone Proofreading The Input". Though it can be rather costly it tends to be highly accurate. ?
Copy linkTweet thisAlerts:
@jrthor2authorJan 08.2007 — this is used for a press release application where business partners enter press releases
×

Success!

Help @jrthor2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...