2012-03-03 3 views
3

Glype 프록시가 있는데 외부 URL을 구문 분석하지 않습니다. 페이지의 모든 URL은 http://proxy.com/browse.php?u=[URL 여기에으로 자동 변환됩니다. 예 : 그래서, 물론Preg-replace - 도메인과 하위 도메인을 제외한 모든 URL을 바꿉니다.

ByteLove.com (Not to: http://proxy.com/browse.php?u=http://bytelove.com&b=0) 
BayFiles.com (Not to: http://proxy.com/browse.php?u=http://bayfiles.com&b=0) 
BayIMG.com (Not to: http://proxy.com/browse.php?u=http://bayimg.com&b=0) 
PasteBay.com (Not to: http://proxy.com/browse.php?u=http://pastebay.com&b=0) 
Ipredator.com (Not to: http://proxy.com/browse.php?u=https://ipredator.se&b=0) 
etc. 

나는 내부 URL을 유지하려면 : 내 프록시에 해적 베이를 방문하면, 나는 다음과 같은 URL을 구문 분석하지 원하는

thepiratebay.se/browse (To: http://proxy.com/browse.php?u=http://thepiratebay.se/browse&b=0) 
thepiratebay.se/top (To: http://proxy.com/browse.php?u=http://thepiratebay.se/top&b=0) 
thepiratebay.se/recent (To: http://proxy.com/browse.php?u=http://thepiratebay.se/recent&b=0) 
etc. 

가 있는가 thepiratebay.se 및 하위 도메인 (예에서와 같이)을 제외한 모든 URL을 대체하기위한 preg_replace? 다른 기능도 환영합니다. (이러한 DOMDocument를, querypath, SUBSTR 또는 strpos로 나는 모든 URL을 정의해야하기 때문에 않는 str_replace하지 않습니다.) 내가 뭔가를 발견했습니다

,하지만 난 preg_replace이다 익숙하지 않다 :

$exclude = '.thepiratebay.se'; 
$pattern = '(https?\:\/\/.*?\..*?)(?=\s|$)'; 
$message= preg_replace("~(($exclude)?($pattern))~i", '$2<a href="$4" target="_blank">$5</a>$6', $message); 

답변

1

난 당신이 도메인 프록시해야하는 알려줄 수있는 화이트리스트를 제공해야합니다 생각합니다 : 그것은 작동하지 않습니다

$whitelist = array(); 
$whitelist[] = "internal1.se"; 
$whitelist[] = "internal2.no"; 
$whitelist[] = "internal3.com"; 
// and so on... 

$string = '<a href="http://proxy.org/browse.php?u=http%3A%2F%2Fexternal1.com&b=0">External link 1</a><br>'; 
$string .= '<a href="http://proxy.org/browse.php?u=http%3A%2F%2Finternal1.se&b=0">Internal link 1</a><br>'; 
$string .= '<a href="http://proxy.org/browse.php?u=http%3A%2F%2Finternal3.com&b=0">Internal link 2</a><br>'; 
$string .= '<a href="http://proxy.org/browse.php?u=http%3A%2F%2Fexternal2.no&b=0">External link 2</a><br>'; 

//Assuming the URL always is inside '' or "" you can use this pattern: 
$pattern = '#(https?://proxy\.org/browse\.php\?u=(https?[^&|\"|\']*)(&?[^&|\"|\']*))#i'; 

$string = preg_replace_callback($pattern, "my_callback", $string); 

//I had only PHP 5.2 on my server, so I decided to use a callback function. 
function my_callback($match) { 
    global $whitelist; 
    // set return bypass proxy URL 
    $returnstring = urldecode($match[2]); 

    foreach ($whitelist as $white) { 
     // check if URL matches whitelist 
     if (stripos($match[2], $white) > 0) { 
      $returnstring = $match[0]; 
      break; } } 
    return $returnstring; 
} 

echo "NEW STRING[:\n" . $string . "\n]\n"; 
+0

, 이것은 내 코드입니다 : http://pastebin.com/6ML8q7JN URL의 위치는 다음과 같습니다. $ document –

+0

대구가 작동하는지 평가하기 위해 $ document 변수의 내용을 확인해야합니다. –

+0

현재 작동하지만 URL 뒤에 _ & b = 0_이 있습니다. 그것을 고치는 방법? –

1

사용할 수있는 preg_replace_callback()은 매치마다 콜백 함수를 실행합니다. 이 함수에서 일치하는 문자열을 변환해야하는지 여부를 결정할 수 있습니다.

<?php 
$string = 'http://foobar.com/baz and http://example.org/bumm'; 
$pattern = '#(https?\:\/\/.*?\..*?)(?=\s|$)#i'; 
$string = preg_replace_callback($pattern, function($match) { 
    if (stripos($match[0], 'example.org/') !== false) { 
     // exclude all URLs containing example.org 
     return $match[0]; 
    } else { 
     return 'http://proxy.com/?u=' . urlencode($match[0]); 
    } 
}, $string); 

echo $string, "\n"; 

(예 PHP 5.3 폐쇄 표기법을 사용한다)

관련 문제