Wonko the Sane Wonko the Sane - 1 year ago 63
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:


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 Source

The rtrim function should work for you.

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

Demo: https://eval.in/627045