複製できる場所があり/
ます。たとえば、次のすべてのリンクから質問にアクセスできます。
/
ここで違いを生むのはだけなhttp://
ので、考えてみましょう。rtrim
私が提供したほとんどの場合、単独では機能しないので、正規表現を使用しましょう。
解決
$parts = explode('//', $full_url, 2);
$parts[1] = rtrim(preg_replace('@/+@', '/', $parts[1]), '/');
$full_url = implode('//', $parts);
unset($parts);
ライブテスト: http://ideone.com/1qHR9o
Before: https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes/
After: https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes
---------------------
Before: https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes////
After: https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes
---------------------
Before: https://stackoverflow.com///questions///13990256///remove-duplicate-trailing-slashes////
After: https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes
---------------------
Before: https://stackoverflow.com/questions//13990256/remove-duplicate-trailing-slashes//
After: https://stackoverflow.com/questions/13990256/remove-duplicate-trailing-slashes
---------------------
説明
あなたの質問から、あなたは常に完全な URL を取得していることを理解しています。したがって、それを 2 つの部分に分割できます。
$parts = explode('//', $full_url, 2);
次に、重複/
したものを次のように削除します。
preg_replace('@/+@', '/', $parts[1])
/
次に、文字列の末尾から余分なものを削除します。
$parts[1] = rtrim( /*previous line*/ , '/');
そしてそれを内破します:
$full_url = implode('//', $parts);
unset($parts);