正则表达式检查有效网址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 answersI 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"); } ?>更多推荐
发布评论