Jach Many
Jach Many

Reputation: 363

removing all trailing slashes in an url

I have written a preg_match code to remove all trailing slashes and that is as follows..

preg_replace("/(\/?)+$/",'',$_SERVER['REQUEST_URI']);

will this hold good any time.

what is the alternatives to remove all trailing slashes...

consider that the users would give sitename.com/folder/ or sitename.com/folder// or sitename.com/folder

Upvotes: 1

Views: 928

Answers (2)

Vishnu Sharma
Vishnu Sharma

Reputation: 1383

See bellow:

<?php
$url = "http://example.co/4569128/removing-all-trailing-slashes-in-an-url/" 

$newUrl = rtrim($url','/'); 

?>
Output : http://example.co/4569128/removing-all-trailing-slashes-in-an-url

------------------------------------------------------------------------

Upvotes: 0

codaddict
codaddict

Reputation: 454950

You can do:

$path = rtrim($path','/');  

Upvotes: 9

Related Questions