PHP 网址验证错误:http://https://example.com(又名 https://https://example.com) [英] PHP Url Validation Error: http://https://example.com (aka https://https://example.com)

查看:64
本文介绍了PHP 网址验证错误:http://https://example.com(又名 https://https://example.com)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 url 正则表达式模式:

I had this url regex pattern in place:

$pattern = "@\b(https?://[^\s()<>\[\]\{\}]{1,".$max_length_allowed_for_each_url."}(?:\([\w\d]+\)|([^[:punct:]\s]|/)))@";

它似乎可以很好地验证我扔给它的任何 URL,直到我意识到 https://http://google.com(显然即使是 stackoverflow 也在考虑一个有效的 URL(它使该 URL 可点击,而不是我,虽然它确实删除了一个冒号)所以也许我运气不好?)一个有效的 URL,当它肯定不是时.

It seemed to work pretty well at validating any URL I threw at it, until I realized that https://http://google.com (apparently even stackoverflow is considering that a valid URL (it made that URL clickable, not me, although it did remove one of the colons) so perhaps I am out of luck?) was a valid URL, when it certainly is not.

我做了一些研究...发现我应该使用 filter_var 而不是正则表达式来验证 PHP URL 无论如何......并且很失望地意识到它也容易受到同样的验证问题的影响.

I did a little research... and learnt that I should be using filter_var instead of a regex for PHP URL validation anyways... and was disappointed to realize that it too is susceptible to this very same validation problem.

我可以轻松地征服它:

str_replace(array("https://http://","http://https://"), array("http://","https://"), $url);

但是……这似乎大错特错.

But... that just seems so wrong.

推荐答案

嗯,这是一个有效的 URI.从技术上讲.如果您不相信我,请查看 RFC 以获取 URI.

Well, it is a valid URI. Technically. Look at the RFC for URIs if you don't believe me.

  • URI 的路径组件可以包含 //.
  • http 是一个有效的主机名.
  • 即使 : 存在(指定为 *digit,而不是 1*digit),也允许缺少端口.(这就是 Stack Overflow 删除冒号的原因——它认为您使用的是默认端口,所以它从 URI 中删除了它.)
  • The path component of a URI can contain //.
  • http is a valid host name.
  • The port is allowed to be missing even if the : is present (it's specified as *digit, not 1*digit). (This is why Stack Overflow removed the colon -- it thought you were using the default port, so it removed it from the URI.)

我建议为此写一个特例.在单独的步骤中,检查 URI 是否以 https?://https?:// 开头,然后修复它.

I suggest writing a special case for this. In a separate step, check to see if the URI starts with https?://https?://, and fix it.

这篇关于PHP 网址验证错误:http://https://example.com(又名 https://https://example.com)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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