Home » Php » php – How to remove the first & last slash from a url if it's exists?

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&Answers:

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