Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the first & last slash from a url if it's exists?

Tags:

regex

php

I use parse_url() to get the path of a url but i want to remove the first slash & the last if it's exists.

Example:

"/posts/funny-dog/" -> "posts/funny-dog"
"/" -> "" // Because http://www.google.com/ path is a slash
"/categories/politic" -> "categories/politics"

Thanks

like image 566
CodeOverload Avatar asked Jun 01 '10 18:06

CodeOverload


People also ask

How do you get rid of the first character in Excel?

1. Combine RIGHT and LEN to Remove the First Character from the Value. Using a combination of RIGHT and LEN is the most suitable way to remove the first character from a cell or from a text string. This formula simply skips the first character from the text provided and returns the rest of the characters.


1 Answers

trim( $str, "/" ); is probably your best/fastest bet.

like image 101
CharlesLeaf Avatar answered Nov 05 '22 11:11

CharlesLeaf