+2 votes
10.3k views
in General by
How to replace all last occurrence of specific character of a string? for example if I want to replace all the characters (say "-") from the end of a sentence?

eg. "Hello world--- This is me----"

I only need the end all "-" characters to be removed, so string will finally be just

"Hello world--- This is me"

Is there a php function like trim() or please suggest how can I do this?

2 Answers

0 votes
by
You may use the "rtrim" for this purpose.

$mytext = "Hello world--- This is me----";

echo rtrim($mytext,"-");

This will replace all the occurrence of the said character to the right end of the string.
0 votes
by (280 points)

Please try preg_replace('/-*$/', '', $var);

Related questions

0 votes
1 answer 8.5k views
asked Mar 5, 2018 in General by mithun (1.3k points)
0 votes
1 answer 633 views
asked Oct 29, 2021 in General by anonymous
0 votes
1 answer 4.2k views
0 votes
1 answer 1.3k views
0 votes
1 answer 680 views
asked Jan 23, 2022 in General by Sarath
0 votes
1 answer 622 views
asked Oct 29, 2021 in General by anonymous
...