2014-02-19 5 views
0

나는 이것을 성취하려고합니다. 나는 (예를 들어) 이와 비슷한 HTML을 많이 가지고있다.PHP는 문자열을 바꾸고 전체 문자열을 읽습니다.

<div> 
    <img src="http://firstsite.com/path/to/img/main.jpg" style="width: 500px; height: 400px;" /> 
</div> 

지금 내가 자동으로 다른 웹 사이트에 이미지의 경로를 변경하는 PHP를 만들기 위해 노력하지만, 나 또한 이미지를 다운로드하고 같은 폴더 구조로 넣어 싶습니다. 지금까지 내가 이런 짓을 :

$input = "c:/wamp/www/primo/input12"; 
    $output = "c:/wamp/www/primo/output12"; 


    $handle = opendir($input); 
    while (($file = readdir($handle)) !== false) { 
     if($file != '.' && $file != '..') { 

      $data = file_get_contents($input . "/" . $file); 

      $data = str_replace("http://firstsite.com/", "http://secondsite.com", $data); 

      file_put_contents($output . "/" . $file, $data); 

     } 
    } 
    closedir($handle); 

이 경로를 변경하지만 지금은 내가 어떻게 든 이미지를 다운로드하기 위해 내 예제에서 변수로 전체 경로 http://firstsite.com/path/to/img/main.jpg를 얻을 필요가있다.

경로의 처음 부분 인 http://firstsite.com/을 바꾸면서 이미지의 전체 경로를 얻을 수있는 방법이 있습니까?

미리 감사드립니다. Daniel!

+0

. 정규 표현식의 도움으로 ... – Havelock

+0

php domdocument를 사용합니다 –

답변

1

받기 이미지 만 :

$data = file_get_contents($input . "/" . $file); 

preg_match_all('/\<img.*src=\"(.+?)\"/s', $data, $matches); 
//go through the match array and download your files 

$data = str_replace("http://firstsite.com/", "http://secondsite.com", $data); 
file_put_contents($output . "/" . $file, $data); 

모든하는 Pathes를 가져옵니다 : 대부분의 아마

$data = file_get_contents($input . "/" . $file); 

preg_match_all('/http\:\/\/firstsite\.com([^\s]+?)/s', $data, $matches); 
//go through the match array and download your files 

$data = str_replace("http://firstsite.com/", "http://secondsite.com", $data); 
file_put_contents($output . "/" . $file, $data); 
+0

정확하게 문제를 이해 한 것 같지만 문제는 그렇지 않다. 모든 html 요소는 imgs입니다. 다른 파일과 다른 경로가 있으므로 http://firstsite.com/에서 경로 검색을 시작합니다. –

+0

업데이트 된 답변보기 – Manu

+1

대단히 감사합니다. Manu, i 've는 모든 경로와 burgeris 정규 표현식을 얻기 위해 코드로 PHP를 작동 시켰습니다. –

1

방법에 대해 :

preg_match_all('/(http:\/\/firstsite\.com\/[^\s]*)/', $data, $matches); 
+1

구분 기호로 실패합니다 –

+0

무엇을 의미합니까? – bargoras

+1

나는 도망치지 않은 구획 문자 (편집 후 바꾼 것) –

관련 문제