2013-04-12 4 views
0

와 URL에 대한 I에는 다음과 같은 세 가지 가능한 URL을 ...경우 /과 문 strpos

  • www.mydomain.com/445/loggedin/?status=empty
  • www.mydomain.com/445/loggedin /? 상태 = 완료 될 www.mydomain.com/445 부분은 동적으로 생성 된 각각의 시간이 그래서는 할 수없는 차이가있다

  • www.mydomain.com/445/loggedin/ 정확한 일치, 어떻게하면 다음을 발견 할 수 있습니까?

    • $ URL이 loggedin이 포함되어 있지만 빈 /? 상태 = OR /? 상태 중 하나를 포함하지 않는 경우 = 전체

    내가 시도 모두가 항상이 부분에 로그인 감지 상관없이 같은 실패 .. 세그먼트

    $path = explode('/',$referrer); 
    $path = array_slice($path,1); 
    

    그럼 그냥 그 배열에 논리를 사용, 전나무에 URL까지

    if(strpos($referrer, '?status=empty')) { 
    echo 'The status is empty'; 
    } 
    elseif(strpos($referrer, '?status=complete')) { 
    echo 'The status is complete'; 
    } 
    elseif(strpos($referrer, '/loggedin/')) { 
    echo 'The status is loggedin'; 
    } 
    
  • 답변

    1

    슬라이스 당신이 반환 포함 t의 URL은 :

    Array ([0] => 445 [1] => loggedin [2] => ?status=empty) 
    
    1

    당신이 뭔가를 할 수 있습니다 : 나는 array_intersect보고하는 것이 좋습니다 것

    $referrer = 'www.mydomain.com/445/loggedin/?status=empty'; 
    
    // turn the referrer into an array, delimited by the/
    $url = explode('/', $referrer); 
    
    // the statuses we check against as an array 
    $statuses = array('?status=complete', '?status=empty'); 
    
    // If "loggedin" is found in the url, and count the array_intersect matches, if the matches = 0, none of the statuses you specified where found 
    if(in_array('loggedin', $url) && count(array_intersect($url, $statuses)) == 0) 
    { 
        echo 'The user is logged in'; 
    } 
    // if the complete status exists in the url 
    else if(in_array('?status=complete', $url)) 
    { 
        echo 'The status is complete'; 
    } 
    // if the empty status exists in the url 
    else if(in_array('?status=empty', $url)) 
    { 
        echo 'The status is empty'; 
    } 
    

    , 그것은 아주 유용하다.

    도움이 되었으면 좋겠지 만 이것이 최선의 방법인지는 모르겠지만 상상력을 자극 할 수 있습니다.

    0

    Strpos는 아마도이 용도로 사용하지 않을 것입니다. 당신은 stristr와 함께 할 수 있습니다 :

    if($test_str = stristr($referrer, '/loggedin/')) 
        { 
         if(stristr($test_str, '?status=empty')) 
         { 
          echo 'empty'; 
         } 
         elseif (stristr($test_str, '?status=complete')) 
         { 
          echo 'complete'; 
         } else { 
          echo 'logged in'; 
         } 
        } 
    

    을하지만 정규 표현식을 할 아마도/쉽게 더 나은 :

    if(preg_match('/\/loggedin\/(\?status=(.+))?$/', $referrer, $match)) 
    { 
        if(count($match)==2) echo "The status is ".$match[2]; 
        else echo "The status is logged in"; 
    }