0

I want to remove ,  from the below string. Here what I have tried

$str = "3gp, ";
echo rtrim($str,', ');

But the output is 3g

It always trims the character p.

I'm using PHP 7.1. Any idea on what's happening?

Edit: it trims 'n', 'b', 's', 'p'

Update: rtrim() removes characters from the last word. If I put a space between p and , i.e $str = "3gp , "; then it won't trim the character 'p'.

Michel
  • 1,085
  • 13
  • 24
  • If you want to trim a phrase instead of characters, don’t use `rtrim()`, you can use `preg_replace()`: `print preg_replace("/, $/", "", $str);` – RatajS Aug 14 '21 at 12:57
  • https://stackoverflow.com/questions/2521051/trim-nbsp-with-php/12718471 – User863 Aug 14 '21 at 13:03

1 Answers1

1

The rtrim() function trims all characters in the ', ' string. This includes a p.

The second parameter represents characters. The manual states:

You can also specify the characters you want to strip, by means of the characters parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.

Martin
  • 22,212
  • 11
  • 70
  • 132
KIKO Software
  • 15,283
  • 3
  • 18
  • 33