Why does rtrim remove things that aren't on the right?

Viewed 105
$word = "shshsh.shshsh";
print(rtrim($word, "sh."));

Output is an empty string when I would have expected it to output the entire string (since "sh." does not appear at the end in this case).

2 Answers

The "sh." is a set of characters rather than a string to remove. It'll remove characters from the end of the $word recursively as long as it's a character in [s, h, .], until it hits one that isn't in this list, essentially removing everything in your case.

From here, the 2nd parameter to rtrim will specify a character mask to remove from the string. In other words, by specifying ".sh" as your character mask, you will remove all instances of ., s, and h separately, yielding the empty string.

Related