Wonko the Sane Wonko the Sane - 3 months ago 17
PHP Question

How to remove apostrophe s ('s) from end of string

I would like to remove "'s" from end of string, but cannot get it to work in PHP.

I found this on SO:

function endsWith($haystack, $needle) {
// search forward starting from end minus needle length characters
return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
}


I tried calling it, like this:

endsWith($word,"'s");


Unfortunately it doesn't work for words that end with 's, such as:

$word = "mom's";
$word = "dad's";


Any suggestion much appreciated. Regex is an option too, I'm not fussy!

EDIT: removed extra ' from "mom's" (sorry!)

Answer

The rtrim function should work for you.

echo rtrim($word,"'s");

Demo: https://eval.in/627045

Comments