PHP & Possessive Punctuation
Automation is great -- it means less work for the developer, the user, and the server. Everyone's happy, right? Unfortunately computers don't pay attention to detail unless they're told to. One detail we need to program computers to pay attention to is proper apostrophe usage when it comes to possession. For example, "Chris's" is not correct -- when a name ends in "s", no "s" should follow the apostrophe. Coding proper English is simple.
The PHP
function properize($string) {
return $string.'\''.($string[strlen($string) - 1] != 's' ? 's' : '');
}
echo 'Properize: '.properize("Chris"); //returns Chris'
echo 'Properize: '.properize("David"); //returns David's
Just a quick tip I wanted to pass on. Details, details, details!
Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...
Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...
Overflow with text is always a big issue, especially in a programmatic environment. There's always only so much space but variable content to add into that space. I was recently working on a table for displaying user information and noticed that longer strings were...
The prospect of creating graphics charts with JavaScript is exciting. It's also the perfect use of JavaScript -- creating non-essential features with unobtrusive scripting. I've created a mix of PHP (the Analytics class), HTML, and MooTools JavaScript that will connect to Google Analytics...
Nice func. Will save me some time. Makes me wonder why I didn’t think of this.
@Timothy becoz you are not as intelligent as David and also not a good programmer as him.
Actually, it would be correct: http://www.bartleby.com/141/strunk.html#1
@Charles: In a word….rubbish.
Chris’s is proper English, and Chris’, although an acceptable alternative, is still technically improper and most sources will advise you to use the former. The rule is that any singular possessive noun ends in ‘s, regardless of whether or not the non-possessive form ends in an s.
See the following (or just Google it):
http://owl.english.purdue.edu/owl/resource/621/01/
http://www.grammarbook.com/punctuation/apostro.asp
http://www.englishclub.com/esl-articles/possessive-apostrophe.htm
What about plural possession?
The belongings of the people, or the peoples’ belongings.
Often gibbs of wood have rough edges. Wood gibbs’ edges are often rough.
@Chris the Developer: That’s a bit out of scope for this function.
@Derrick: That goes against everything I’ve ever been taught (and I got A’s in every English class I’ve taken since 5th grade.) I wonder if we teach it different in the US.
Another U.S. professional editor here, and I use primarily the Chicago Manual of Style, which is a standard reference. It says the same: that Chris’s would be correct. See section 7.17 on possessives. The funny part in that section is this: “Since feelings on these matters sometimes run high, users of this manual may wish to modify or add to the exceptions.”
Maybe your teachers all had “high” feelings:)
See http://www.chicagomanualofstyle.org
Just wanted to add that it’s a cool tip, though, and I read your blog every day…
@David Walsh: I don’t know what kind of school you went to… that’s not what I learned. In my book, “Chris’s” is correct. Also, the plural form of “A” is “As”, not “A’s”.
@Chris the Developer: The plural possessive of people is “people’s”. You chose a tricky example since people is an irregular pluralization. However, in general, you are correct that the apostrophe comes after the “s” in plural possessives (e.g. teachers’ belongings).
The rule I was always taught was that if the name/object ended in an “s”, you put ONLY an apostrophe after the word. I’ll be right back — I’m going to go sue my old school district.
David, my school district said the same thing!
This is pretty clever. I’ve only made helper functions for pluralizing a word.
Yea mine taught the same thing. I think it’s more of an understanding that we use that rather than a rule. If ever see a site that uses my name in plurals like this “James’s” I’m going to look at it funny.
Although I’m not sure, I think I was taught the same way as David was. Personally I think “…s’s” looks awkward.
It might not be correct, but when working with programming terms and acronyms I always insert an apostrophe before the S. Sometimes the meaning would change without one.
Uh… wouldn’t it be, like… a lot faster if you manually typed out the apostrophe?
@Jam: Can’t do that in an automated system dogg.
I prefer using substr($string, -1) to get the last character instead of the array, strlen() method you use.
I was taught (public school in New York) that for singular possessive, always add the ‘s.
However, I agree it looks awkward, so I usually use the form with apostrophe only. My own little bit of arrogance.
Yeah, it does look awkward, and that’s why most sources say that it’s acceptable (although not preferable) to use just the apostrophe in those cases. It probably depends on where you’re using it, too. I’d be hesitant to use just the apostrophe on a project for a school, university, or other educational institution :)
You can also use this:
substr ( $string, -1 )
in place of this:
$string[strlen($string) – 1]
which makes for cleaner, more readable code, in my opinion.
Another solution using regxp :
Damn, I just wanted to be cool and say that substr -1 would be way awesomer, but I’ve been beated to it :(
Once again, David Walsh to the rescue with a little snippet.
yet another alternative