2012-01-05 4 views
0

주어진 URL이 최소한 하나의 사이트 이름과 일치하는지 어떻게 확인합니까?일치하는 URL을 확인하는 PHP 정규식

나는이 :

$url_to_match = 'http://sub.somesite.com/'; 

가 난 단지 http://sub.somesite.com로 시작하는 입력에 대해 "MATCH 발견"말하고 싶습니다.

도움이 될 것입니다.

답변

2

사용 PHP의 parse_url() :

$url = 'http://sub.somesite.com/'; 
if ('sub.somesite.com' === parse_url($url, PHP_URL_HOST)) { 
    // we have a match 
} 
+0

+1. OP가 계획을 유효하게하고 싶은지에 달려있다. – cmbuckley

+0

고마워, 잘 했어. 나는 많은 정규식 접근법과 혼동되었다. :) 그러나 이것은 더 간단하다. – swan

0

이 요청은 설계상의 의미가 없으므로 현재 수행중인 작업에 대해 알려 주시면됩니다.

그러나 질문에 대답하십시오.

if(strpos($url_to_match, 'http://sub.anothersite.com/bla') !== FALSE) print 'bad string'; 
+0

이 strpos의 잘못된 사용() (구문 오류)이지만, 예, 기반 질문, strpos()를 사용해야합니다. http://lt.php.net/strpos –

+0

@ AurelijusValeiša, 좋은 캐치, 나는'$ haystack'을 잊어 버렸습니다. XD – Xeoncross

+0

나는 내 질문을 명확히했다. 나는 그 반대를 확인하는 것이 내가 의미했던 것이 아닌가 걱정된다. 작업에 적합한 도구에 대해 감사합니다. – swan

1

사용 parse_url

예 :

function match_url($base,$input) 
{ 
    $base_host = parse_url($base,PHP_URL_HOST); 
    $input_host = parse_url($input,PHP_URL_HOST); 
    if($base_host === $input_host) { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
$base_url = 'http://sub.somesite.com'; 
$input_url = 'http://sub.somesite.com//bla/bla'; 
echo (match_url($base_url,$input_url)) ? "URL matched" : "URL mismatched"; 
+0

고마워요, 이것도 작동해야합니다. – swan