1

Check out this code.

$bt = "abc8 • ";
echo $bt . "<P>";
$bt = rtrim($bt," &#8226; ");
echo $bt . "<P>";

$bt = "abc7 &#8226; ";
echo $bt . "<P>";
$bt = rtrim($bt," &#8226; ");
echo $bt . "<P>";

On my server, running PHP7.2, this returns,

abc8 •

abc

abc7 •

abc7

Why is the "8" being dropped in the first pair???

If I use the actual bullet symbol in the code, rather than the 8226 entity, it works fine.

Community
  • 1
  • 1
Jon Bernhardt
  • 531
  • 1
  • 4
  • 4
  • 1
    because you've listed "8" as one of the characters to be stripped. PHP is seeing what you put in the second argument to rtrim as a list of individual characters. It will find **all** references to each of those separate characters and remove them. See http://php.net/manual/en/function.rtrim.php – ADyson Sep 25 '18 at 20:33
  • So the answer is that I'm a dummy! Ha-ha! Sorry to waste your time, everyone. I usually remember to check the docs first. ;) – Jon Bernhardt Sep 26 '18 at 12:48

1 Answers1

3

You're misunderstanding the purpose of rtrim(). From the documentation:

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

Regular expression or a simple str_replace() would be what you're looking for.

$bt = str_replace(" &#8226; ", "", $bt);

$bt = preg_replace("/\s*&#8226;\s*$/", "", $bt);
miken32
  • 42,008
  • 16
  • 111
  • 154