正则表达式检查有效网址http或www [复制](regex to check valid url either http or www [duplicate])

系统教程 行业动态 更新时间:2024-06-14 16:57:17
正则表达式检查有效网址http或www [复制](regex to check valid url either http or www [duplicate])

这个问题在这里已有答案:

检查字符串是否为有效URL的最佳正则表达式是什么? 45个答案

我想检查用户插入的URL是否有效。 我有不同的情况允许用户插入URL

1) www.test.com (valid) 2) http://test.com (valid) 3) http://www.test.com (valid) 4) www.test (not valid)

这样用户就可以插入www或http ,如果用户只插入www则在URL之前预先附加http:// 。 我找到了许多正则表达式,但他们严格检查http:// 。

提前致谢。

This question already has an answer here:

What is the best regular expression to check if a string is a valid URL? 52 answers

I want to check that user inserted URL is valid or not. I have different cases to allow user to insert URL

1) www.test.com (valid) 2) http://test.com (valid) 3) http://www.test.com (valid) 4) www.test (not valid)

So this way user will able to insert www or http, If user insert only www then pre-append http:// before URL. I found many regex but they strictly check http://.

Thanks in advance.

最满意答案

看到这个链接可能对你有帮助。

<?php // Variable to check $url = "http://www.w3schools.com"; // Validate url if (!filter_var($url, FILTER_VALIDATE_URL) === false) { echo("$url is a valid URL"); } else { echo("$url is not a valid URL"); } ?>

see this link may help you.

<?php // Variable to check $url = "http://www.w3schools.com"; // Validate url if (!filter_var($url, FILTER_VALIDATE_URL) === false) { echo("$url is a valid URL"); } else { echo("$url is not a valid URL"); } ?>

更多推荐

本文发布于:2023-04-12 19:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/7b051ca68c8d97700a4be824684135ba.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网址   正则表达式   www   http   regex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!