PHP trim only the exact string with example

In PHP, trim(), rtrim() and ltrim() functions with character_mask that trim characters in all the possibilities ways of prefix.

$my_text = 'Reading RBI books on travel is a best policy.';
#rtrim($string, character_mask);
echo rtrim($my_text, 'ing');

#output:
Read RB books on travel is a best policy.

Here, Want to trim the sufix ing only but rtrim trimmed "ing" in reading and "I" in RBI.

So using preg_replace to trim only the exact string,
Example Script:

function right_trimmer($val) {
    #It trims s,ed,ly,ing only..
    return preg_replace("/\s|ed|ly|ing$/","",$val);
}

PHP Trim Posted on 2016-05-25 07:14:21