我正在尝试在PHP中编写一个正则表达式来验证给定的URL,而不使用http://或https://或子域只是域名和MUST stat with(www。)。有效的例子是:
www.google.co.uk
www.eample.tv
www.example.com
www.google.com
www.dev.google.com
等。无效的例子是:
wwww.eample.com/test
wwww.example.com?exam
http://wwww.google.com
http://google.com
ftp://www.google.com
http://www.google.com/test/?test
https://dev.example.com/test
dev.example.com ( sub domains that doesnt start with www. are invalid)
等。我该怎么做呢?
答案 0 :(得分:0)
答案 1 :(得分:0)
/^[w]{3}\.[A-z0-9\-]+\.[A-z0-9\.]+$/i
答案 2 :(得分:0)
模式/^www(?:\.[-\w]+)+$/
(www
至少跟随一次,后跟一个破折号或单词字符)与您所有的有效示例匹配,但与您所有的< em>无效的示例。
<?php
$examples = array
( "www.google.co.uk", "www.eample.tv", "www.example.com", "www.google.com",
"www.dev.google.com",
// Invalid examples are:
"wwww.eample.com/test", "wwww.example.com?exam", "http://wwww.google.com",
"http://google.com", "ftp://www.google.com",
"http://www.google.com/test/?test", "https://dev.example.com/test",
"dev.example.com",
);
foreach ($examples as $e)
{
echo "$e is ";
if (!preg_match('/^www(?:\.[-\w]+)+$/', $e)) echo "in";
echo "valid\n";
}
答案 3 :(得分:-2)
试试这个正则表达式: