I have a hard time getting around regular expressions and I’m trying to remove the last forward slash in a string :
$public_url = "https://api.mongohq.com/";
What I intend is remove the last forward slash and replace it with something else. I figured I could use preg_replace but I cannot find the right pattern for doing it.
You can use a negative lookafter-expression:
Output:
https://api.mongohq.comfoobar
Update:
Use the following regex to avoid problems with characters behind the last slash:
All characters behind the last slash are replaced, too. Thanks to knittl!