2012-07-30 3 views
1

나는 PHP에 대해 아무 것도 모른다. 그래서 아마 누군가 웃게 만들 것이다.php preg_match multiple url

index.php에 호스트 헤더를 확인하고 일치하는 항목이 있으면 리디렉션되는이 코드가 있습니다.

if (!preg_match("/site1.net.nz/",$host)) { 
    header('Location: http://www.siteblah.net.nz/temp_internet_block.cfm'); 
} 

그러나 잠재적으로 여러 사이트를 확인해야합니다. 다음과 같이

if (!preg_match("/site1.net.nz/"|"/site2.net.nz",$host)) { 
    header('Location: http://www.siteblah.net.nz/temp_internet_block.cfm'); 
} 

실제로, 나는 :-) 알고

답변

0
// [12] to match 1 or 2 
// also need to escape . for match real . otherwise . will match any char 
if (!preg_match("/site[12]\.net\.nz/",$host)) { 
    header('Location: http://www.siteblah.net.nz/temp_internet_block.cfm'); 
} 

또는

if (!preg_match("/site1\.net\.nz|site2\.net\.nz/",$host)) { 
    header('Location: http://www.siteblah.net.nz/temp_internet_block.cfm'); 
} 
+0

감사하지만 항상 유사한 URL 일 수는 없습니다. 이상적으로, 어쩌면 내가 필요한 곳에 다른 URL을 추가 할 수있는 배열이 필요할지도 모릅니다. – user460114

+0

@ user460114 내 편집을 참조하십시오. – xdazz

1
if (!preg_match("/(site1\.net\.nz|site2\.net\.nz|some\.other\.domain)/",$host)) { 
    header('Location: http://www.siteblah.net.nz/temp_internet_block.cfm'); 
} 
1

한번에 모두를위한 올바른 구문 수 있습니다

$hosts="/(site1\.com)|(site2\.com)/"; 
if (!preg_match($hosts,$host)) { 
    // do something. 
} 
0

올바른 RegEx 구문입니다.

URL 배열이 있다고 가정 해 보겠습니다.

$array = Array('site1.net.nz', 'site2.net.nz'); 

foreach($array as &$url) { 
    // we need to escape the url properly for the regular expression 
    // eg. 'site1.net.nz' -> 'site1\.net\.nz' 
    $url = preg_quote($url); 
} 

if (!preg_match("/(" . implode("|", $array) . ")/",$host)) { 
    header('Location: http://example.com/'); 
}