如何从 PHP 中的用户输入中删除 URL 协议和斜杠
用户输入示例
http://example.com/
http://example.com/topic/
http://example.com/topic/cars/
http://www.example.com/topic/questions/
我想要一个 PHP 函数来使输出如下
example.com
example.com/topic/
example.com/topic/cars/
www.example.com/topic/questions/
Example user input
http://example.com/
http://example.com/topic/
http://example.com/topic/cars/
http://www.example.com/topic/questions/
I want a PHP function to make the output like
example.com
example.com/topic/
example.com/topic/cars/
www.example.com/topic/questions/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
ereg_replace
现已弃用,因此最好使用:这会删除
http://
或https://
ereg_replace
is now deprecated, so it is better to use:This removes either
http://
orhttps://
您应该使用一系列“不允许的”术语并使用
strpos
和str_replace
从传入的 URL 中动态删除它们:You should use an array of "disallowed" terms and use
strpos
andstr_replace
to dynamically remove them from the passed-in URL:我建议使用 PHP 提供的工具,看看 parse_url。
上面的示例将输出:
听起来您至少在
host
+path
(根据需要添加其他内容,例如query
):干杯
I'd suggest using the tools PHP gave you, have a look at parse_url.
The above example will output:
It sounds like you're after at least
host
+path
(add others as needed, e.g.query
):Cheers
创建一个数组:
并替换为空字符串:
它看起来像这样:
如果您的干草堆(输入)是字符串并且您用字符串替换数组中的针,则 Str_replace 将返回一个字符串。这很好,这样您就可以避免所有额外的循环。
Create an array:
and replace with empty string:
it would look something like this:
Str_replace will return a string if your haystack (input) is a string and you replace your needle(s) in the array with a string. It's nice so you can avoid all the extra looping.
哇。我从谷歌来到这里,希望找到一个可以复制和粘贴的衬垫!
您不需要函数来执行此操作,因为已经存在一个函数。只需执行以下操作:
在本例中,explode() 将返回一个数组:
我们想要第二个元素。这将处理 http、https、ftp 或几乎任何 URI,而不需要正则表达式。
https://www.php.net/manual/en/function.explode.php
如果您想要一个功能:
编辑:请参阅 Harry Lewis 的用户评论...这是我现在最喜欢的方法。
Wow. I came here from google expecting to find a one liner to copy and paste!
You don't need a function to do this because one already exists. Just do:
In this example, explode() will return an array of:
And we want the 2nd element. This will handle http, https, ftp, or pretty much any URI, without needing regex.
https://www.php.net/manual/en/function.explode.php
If you want a function:
EDIT: See user comment from Harry Lewis... this is my favourite way to do this now.
您可以使用带有
preg_replace 的正则表达式在一行中删除 https 和 http
:~^https?://~< /code> 测试和解释
(regex101.com)
You can remove both https and http in one line using a regular expression with
preg_replace
:~^https?://~
Test and Explanation (regex101.com)您可以使用 PHP 的解析 url 功能。这适用于所有协议,甚至 ftp:// 或 https://
Eiter 获取协议组件并从 Url 中减去它,或者只是将其他部分连接在一起...
http://php.net/manual/de/function.parse-url.php
You could use the parse url Functionality of PHP. This will work for all Protocols, even ftp:// or https://
Eiter get the Protocol Component and substr it from the Url, or just concatenate the other Parts back together ...
http://php.net/manual/de/function.parse-url.php
这个解决方案对我有用,简短的一个。
This solution works for me, short one.