Is first letter a capital?



Thanks to you guys I sorted out my previous problems.

Now for my next challenge. People send me orders but half of them are total bozos and don't realize that a name is supposed to begin with a capital letter so I'll get an order from "mr dougie mcdougal, 26 wentworth boulevard"... etc.

I can't just copy this into the invoice. I have to spend precious minutes retyping it because there's no way I'm going to send out a letter addressed like a five-year-old typed it!

So, I know how to capitalize words. I even found a script that does it intelligently so that "mcdougal" becomes "McDougal" and not "Mcdougal".

What I can't figure out is an elegant way to determine if the first letter is already capitalized (and the last letter isn't). If this is so, then the chances are that everything else is OK. Hmm, I just thought... the last letter might be a comma! Oh, forget that. Let's just test for the first letter in the name being one of A-Z. If the whole name is in capitals I don't care that much.

I've searched the Manual but I can't find an example that specifically addresses this. Maybe you can point me to a page?

Do I have to check for "A - Z" or is it better to look for the actual ASCII number values (how)? Suggestions welcome!

(Many years ago I knew the ASCII numbers off by heart. Now all I can remember is "return" is ASCII 13 and I think "line feed" is 10 and "space" is 20. And maybe "A" is 64 ... but I'm faltering).

Seems there ought to be a better way, Martin, but the way I'd determine case is just by using strtoupper() and then checking if the "uppered" version is the same as the original:

function is_capital($char) {
   return $char == strtoupper($char);

$name = 'mcDOUGAL';

// check to see if first letter is capitalized
if(is_capital($name{0})) {
   echo "First letter IS capitalized.<br/>\n";
} else {
   echo "First letter IS NOT capitalized.<br/>\n";

// check to see if last letter is capitalized
if(is_capital($name{strlen($name)})) {
   echo "Last letter IS capitalized.<br/>\n";
} else {
   echo "Last letter IS NOT capitalized.<br/>\n";

/* output:
      First letter IS NOT capitalized.
      Last letter IS capitalized.

That ought to do it. Like I said, it seems like there should be a better way, but my philosophy is to always use built-in functions when possible, and I can't find any built-in functions that I think would do the job better.

One potential snafu with the above function: If $char happens to be a non-alpha character, it will always return true (because strtoupper() doesn't do anything to characters that can't be converted to uppercase). So you'll have to do your own checking for that, but I assume you do anyway.
Ooh that's a clever method! I would never have thought of doing it that way! A clear case of "lateral thinking". (As a schoolteacher of mine used to say over 35 years ago, "I bet you've got brains you haven't used yet".)

I don't think there's much chance of the first letter being anything other than a letter of the alphabet. Mind you, some of my customers are so illiterate I wonder if they ever went to school. :?

Thanks. I'm going to bed but I'll try your brilliant idea in the morning.

This stuff isn't necessary for the running of my business,of course, but it's a good way of learning to use PHP with practical uses.

BTW if you are wondering who the heck I am, you can read about me here:

(But don't believe everything).

Well, you were close!

// check to see if first letter is capitalized
if(is_capital($name{1})) {

Should be a zero {0} to take the first letter.

Heck, I didn't know that but the first letter is apparently in position zero, not position one. Had me fooled for a moment when it didn't work!

Thanks for your help once again. :)

It's OK. I don't expect "answers on a plate". I can figure out the simple stuff myself. I just need to be pointed in the right direction! :)