php 从 URL 的尾部删除正斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1030661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:42:27  来源:igfitidea点击:

Removing a forward-slash from the tail-end of an URL

php

提问by John Kugelman

The code below removes "www.", etc. from the beginning of websites that are entered into a database. It works great.

下面的代码从进入数据库的网站的开头删除“www.”等。它工作得很好。

Is there a way I could use similar code to remove a forward-slash from the tail-end of a website that is entered into the same database?

有没有办法可以使用类似的代码从输入到同一数据库的网站的尾部删除正斜杠?

$remove_array = array('http://www.', 'http://', 'https://', 'https://www.', 'www.');
$site = str_replace($remove_array, "", $_POST['site']);

回答by Daniel Vandersluis

You can pass a string of characters that you want trimmed off of a string to the trimfamily of functions. Also, you could use rtrimto trim just the end of the string:

您可以将要从字符串中修剪掉的字符串传递给trim函数系列。此外,您可以使用rtrim仅修剪字符串的末尾:

$site = rtrim($site, "/");

回答by John Kugelman

$site = preg_replace('{/$}', '', $site);

This uses a relatively simple regular expression. The $means only match slashes at the end of the string, so it won't remove the first slash in stackoverflow.com/questions/. The curly braces {}are just delimiters; PHP requires matching characters and the front and back of regular expressions, for some silly reason.

这使用了一个相对简单的正则表达式。这$意味着只匹配字符串末尾的斜杠,所以它不会删除stackoverflow.com/questions/. 花括号{}只是分隔符;出于某种愚蠢的原因,PHP 需要匹配字符以及正则表达式的前后。

回答by Stephanie Gratzer

Simplest method:

最简单的方法:

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

回答by merkuro

John was the first and I think his solution should be preferred, because it's way more elegant, however here is another one:

John 是第一个,我认为他的解决方案应该是首选,因为它更优雅,但这里是另一个:

$site = implode("/", array_filter(explode("/", $site)));

Update

更新

Thx. I updated it and now even handles things like this

谢谢。我更新了它,现在甚至可以处理这样的事情

$site = "///test///test//"; /* to => test/test */

Which probably makes it even cooler than the accepted answer ;)

这可能使它比接受的答案更酷;)

回答by Piotr Salaciak

Is that what You want?

那是你要的吗?

$url = 'http://www.example.com/';

if (substr($url, -1) == '/')
    $url = substr($url, 0, -1);

回答by alex

The most elegant solution is to use rtrim().

最优雅的解决方案是使用rtrim()

$url = 'http://www.domain.com/';

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

EDIT

编辑

I forgot about rtrim();

我忘记了 rtrim();

You could also play around parse_url().

你也可以玩转parse_url()

回答by inkfist

$new_string = preg_replace('|/$|', '', $string);

回答by John Hamelink

Perhaps a better solution would be to use .htaccess, but php can also do it with something like this:

也许更好的解决方案是使用 .htaccess,但 php 也可以使用这样的方法:

<?php
    header('location: '.preg_replace("/\/$/","",$_SERVER['REQUEST_URI']));
?>