Hi,

I need to add an apostrophe or apostrophe "s" to a name. I don't know how to get the last character in the string.

For instance:

if the name is John (or ends in anything other than "s") - I want to output "John's email".

If the name is Sparticus (or ends in "s") - I want to output "Sparticus' email".

Any help, insight, or direction is greatly appreciated.

Koray

    use substr() with a -1

    if(substr($name, -1) == "s") {
       $newName = $name."'";
       // add the apostrophe
    }
    else { 
       $newName = $name."'s";
       // add 's
    }
    

      Unfortunately, there is a problem with this. One-syllable names ending in 's' take an apostrophe s. A simple way around this is a regex.

      So, LordShryku's code could be:

      if(substr($name, -1) == "s") { 
         if(preg_match("/^[bcdfghjklmnpqrstvwxyz]*[aeiouy]+[bcdfghjklmnpqrstvwxyz]+[aeiouy]+/i", $name)){
            $newName = $name."'";
         } else {
            $newName = $name."'s";
         }  
      } else {
      $newName = $name."'s"; }

      What this new code does is it tries to check if a word is two syllables or longer. However, this may not always work, but is definitely better than nothing.

        I also added a slight protection for names ending in apostrophe. I have a friend named che' - so thought it might be a good idea. I suppose the script can be developed for alot of instances (exceptions) but will work for my purposes for the time being. I did place it in a function so that I can continue developing it. Thanks for the replies.

        function possessMe($pm_source) {
        	// grab last character
        	$pm_source_lastChar = substr($pm_source,_-1);
        	// protection against accent
        	if ($pm_source_lastChar != "'") {
        		// determine if s or other
        		if ($pm_source_lastChar == "s") {
        			$pm_source = $pm_source . "'";
        		} else {
        			$pm_source = $pm_source . "'s";
        		}
        	} else {
        		$pm_source = $pm_source . "s";
        	}
        	return $pm_source;
        }

        Did not get a chance to include the syllable update yet.

          OK, I only have 9 credits of linguistics to my credit (10 years ago or so) but it seems that regex would be incapable of detecting a syllable since syllabation is a phonological rather than orthographic thing, i.e., relates to pronunciation not spelling. How many syllables does 'rhythm' have? (not that i'm sure that breaks your pattern) ... anyway just a thought. Also, I'm not sure what the proper grammar is. I work at a journalism school so we adhere to AP style for the most part, which I think says either XXXs' or XXXs's is fine. But whatever.

            Write a Reply...