更好的方法来验证PHP中的URL [英] Better way to validate a URL in PHP

查看:142
本文介绍了更好的方法来验证PHP中的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读堆栈溢出的各种帖子,试图找到一个理想的方式来验证在PHP中的URL。我的研究已经提出了三种可能的解决方案,但是,没有一种是理想的。

提到的三个方法是正则表达式, filter_var($ url,FILTER_VALIDATE_URL) parse_url ()。第一种方法的问题已经众所周知,最全面的验证正则表达式我可以找到跨页面。内置到PHP过滤扩展中的filter_var函数似乎有错误,比如将http:// ...这样的url视为有效。 parse_url方法使用的是从未用于URL验证的函数,因此不能依赖于此任务。



有没有关于PHP中的URL验证的其他选项,我可能错过了?

跨越页面)额外的正则表达式检查你不认为被覆盖的情况吗?

I've been reading various posts on Stack Overflow to try and find an ideal way to validate a URL in PHP. My research has come up with three possible solutions, however, none of them are ideal.

The three methods mentioned are regex, filter_var ($url, FILTER_VALIDATE_URL) and parse_url (). The problems with the first approach are already well known, and the most comprehensive validation regex I could find spanned pages. The filter_var function built into the PHP filtering extension appears to have faults, such as treating a url like http://... as valid. The parse_url method is using a function that was never intended for URL validation and therefore can't be depended on for this task.

Are there any other options regarding URL validation in PHP that I may have missed?

解决方案

How about you combine filter_var (which basically is a regex check that spans pages) with additional regex check for cases you don't think are covered well by it?

这篇关于更好的方法来验证PHP中的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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