从URL的末尾删除正斜杠 [英] Removing a forward-slash from the tail-end of an URL

查看:89
本文介绍了从URL的末尾删除正斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码从输入数据库的网站的开头删除"www."等.效果很好.

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

是否可以使用类似的代码从输入相同数据库的网站的尾部删除正斜杠?

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']);

推荐答案

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

这使用了相对简单的正则表达式. $表示仅在字符串末尾匹配斜杠,因此不会删除stackoverflow.com/questions/中的第一个斜杠.花括号{}只是定界符;由于某些愚蠢的原因,PHP需要匹配的字符以及正则表达式的前后.

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.

这篇关于从URL的末尾删除正斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆