Replace all last occurrence of characters of string

+2 votes
210 views
asked Jun 3, 2016 in General by Neethu
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?

1 Answer

0 votes
answered Jun 3, 2016 by Kamal
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.

Related questions

0 votes
1 answer 298 views
+1 vote
1 answer 112 views
+2 votes
1 answer 107 views
asked Feb 9 in General by Marc Lentin
0 votes
2 answers 179 views
asked Nov 24, 2016 in General by srinivasulu_yagnam (200 points)
...