我已经实现了一个验证 .edu 域的功能。这就是我的做法:
if( preg_match('/edu/', $matches[0])==FALSE )
return FALSE;
return TRUE;
现在我想跳过那些指向某些文档(例如 .pdf 和 .doc)的 url。
为此,以下代码应该可以工作,但不能:
if( preg_match('/edu/', $matches[0])==FALSE || preg_match('/pdf/i', $matches[0])!=FALSE || preg_match('/doc/i', $matches[0]!=FALSE))
return FALSE;
return TRUE;
我在这方面哪里错了?此外,我将如何实现 preg_match 以使其具有要检查 url 字符串的文档类型列表。如果找到某种类型的文档,它应该返回 false。换句话说,我想提供各种文档类型的列表(可能是数组)作为 $pattern 以在 url 中查找。
注意:matches[0] 包含整个 url 字符串。例如:http ://www.nust.edu.pk/Documents/pdf/NNBS_Form.pdf
该函数的代码:
public function validateEduDomain($url) {
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);
$host = $matches[1];
// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
if( preg_match('/edu/', $matches[0])!=FALSE && (preg_match('/pdf/i', $matches[0])==FALSE || preg_match('/doc/i', $matches[0]==FALSE)))
return TRUE;
return FALSE;
}