<?php
function rtrim2($str, $charlist=null)
{
$str = (string)$str;
$charlist = (string)$charlist;
$offset = strlen($str) - $len; while($offset > 0 && $offset == strpos($str, $charlist, $offset)) {
$str = substr($str, 0, $offset); $offset = strlen($str) - $len; }
}
$string = "bubu foi almoçar<br>";
echo rtrim2($string, "<br>") . PHP_EOL;
$string = "bubu foi almoçar<hr>";
echo rtrim2($string, "<hr>") . PHP_EOL;
$string = "bubu foi almoçar!";
echo rtrim2($string, "!") . PHP_EOL;
$string = "bubu foi almoçar";
echo rtrim2($string, "<br>") . PHP_EOL;
PD9waHAKCmZ1bmN0aW9uIHJ0cmltMigkc3RyLCAkY2hhcmxpc3Q9bnVsbCkgCnsgCiAgICAkc3RyICAgICAgPSAoc3RyaW5nKSRzdHI7IAogICAgJGNoYXJsaXN0ID0gKHN0cmluZykkY2hhcmxpc3Q7ICAgIAogICAgCiAgICBpZihlbXB0eSgkY2hhcmxpc3QpKSAKICAgICAgIHJldHVybiBydHJpbSgkc3RyKTsgCiAgICAKICAgICRsZW4gPSBzdHJsZW4oJGNoYXJsaXN0KTsgCiAgICAkb2Zmc2V0ID0gc3RybGVuKCRzdHIpIC0gJGxlbjsgCiAgICB3aGlsZSgkb2Zmc2V0ID4gMCAmJiAkb2Zmc2V0ID09IHN0cnBvcygkc3RyLCAkY2hhcmxpc3QsICRvZmZzZXQpKSAKICAgIHsgCiAgICAgICAgJHN0ciA9IHN1YnN0cigkc3RyLCAwLCAkb2Zmc2V0KTsgCiAgICAgICAgJG9mZnNldCA9IHN0cmxlbigkc3RyKSAtICRsZW47IAogICAgfSAKICAgIAogICAgcmV0dXJuIHJ0cmltKCRzdHIpOyAgICAKfQoKJHN0cmluZyA9ICJidWJ1IGZvaSBhbG1vw6dhcjxicj4iOwplY2hvIHJ0cmltMigkc3RyaW5nLCAiPGJyPiIpIC4gUEhQX0VPTDsKCiRzdHJpbmcgPSAiYnVidSBmb2kgYWxtb8OnYXI8aHI+IjsgICAgCmVjaG8gcnRyaW0yKCRzdHJpbmcsICI8aHI+IikgLiBQSFBfRU9MOwoKJHN0cmluZyA9ICJidWJ1IGZvaSBhbG1vw6dhciEiOwplY2hvIHJ0cmltMigkc3RyaW5nLCAiISIpIC4gUEhQX0VPTDsKCiRzdHJpbmcgPSAiYnVidSBmb2kgYWxtb8OnYXIiOwplY2hvIHJ0cmltMigkc3RyaW5nLCAiPGJyPiIpIC4gUEhQX0VPTDs=