PHP & Possessive Punctuation

By  on  

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!

Recent Features

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

Incredible Demos

  • By
    Using MooTools For Opacity

    Although it's possible to achieve opacity using CSS, the hacks involved aren't pretty. If you're using the MooTools JavaScript library, opacity is as easy as using an element's "set" method. The following MooTools snippet takes every image with the "opacity" class and sets...

  • By
    Cross Browser CSS Box Shadows

    Box shadows have been used on the web for quite a while, but they weren't created with CSS -- we needed to utilize some Photoshop game to create them.  For someone with no design talent, a.k.a me, the need to use Photoshop sucked.  Just because we...

Discussion

  1. Nice func. Will save me some time. Makes me wonder why I didn’t think of this.

  2. saiful

    @Timothy becoz you are not as intelligent as David and also not a good programmer as him.

  3. Charles

    “Chris’s” is not correct

    Actually, it would be correct: http://www.bartleby.com/141/strunk.html#1

  4. feedbacker

    @Charles: In a word….rubbish.

  5. Derrick

    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

  6. Chris the Developer

    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.

  7. @Chris the Developer: That’s a bit out of scope for this function.

  8. @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.

  9. 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

  10. Just wanted to add that it’s a cool tip, though, and I read your blog every day…

  11. @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).

  12. 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.

  13. David, my school district said the same thing!

    This is pretty clever. I’ve only made helper functions for pluralizing a word.

  14. 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.

  15. Jani Peltoniemi

    Although I’m not sure, I think I was taught the same way as David was. Personally I think “…s’s” looks awkward.

  16. Darkimmortal

    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.

  17. Jam

    Uh… wouldn’t it be, like… a lot faster if you manually typed out the apostrophe?

  18. @Jam: Can’t do that in an automated system dogg.

  19. Graham McNicoll

    I prefer using substr($string, -1) to get the last character instead of the array, strlen() method you use.

  20. 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.

  21. Derrick

    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.

  22. Another solution using regxp :

    function properize($string) {
    return $string.(preg_match('/[s]$/', $string) ? "'" : "'s");
    }
  23. marcusklaas

    Damn, I just wanted to be cool and say that substr -1 would be way awesomer, but I’ve been beated to it :(

  24. Once again, David Walsh to the rescue with a little snippet.

  25. Jarik

    yet another alternative

    ...
    return $string . (str_ends_with($string, 's') ? '\'' : '\'s');
    ...
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!