주어진 URL이 최소한 하나의 사이트 이름과 일치하는지 어떻게 확인합니까?일치하는 URL을 확인하는 PHP 정규식
나는이 :
$url_to_match = 'http://sub.somesite.com/';
가 난 단지 http://sub.somesite.com
로 시작하는 입력에 대해 "MATCH 발견"말하고 싶습니다.
도움이 될 것입니다.
주어진 URL이 최소한 하나의 사이트 이름과 일치하는지 어떻게 확인합니까?일치하는 URL을 확인하는 PHP 정규식
나는이 :
$url_to_match = 'http://sub.somesite.com/';
가 난 단지 http://sub.somesite.com
로 시작하는 입력에 대해 "MATCH 발견"말하고 싶습니다.
도움이 될 것입니다.
사용 PHP의 parse_url() :
$url = 'http://sub.somesite.com/';
if ('sub.somesite.com' === parse_url($url, PHP_URL_HOST)) {
// we have a match
}
이 요청은 설계상의 의미가 없으므로 현재 수행중인 작업에 대해 알려 주시면됩니다.
그러나 질문에 대답하십시오.
if(strpos($url_to_match, 'http://sub.anothersite.com/bla') !== FALSE) print 'bad string';
사용 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";
고마워요, 이것도 작동해야합니다. – swan
+1. OP가 계획을 유효하게하고 싶은지에 달려있다. – cmbuckley
고마워, 잘 했어. 나는 많은 정규식 접근법과 혼동되었다. :) 그러나 이것은 더 간단하다. – swan