나는 이것을 성취하려고합니다. 나는 (예를 들어) 이와 비슷한 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!
. 정규 표현식의 도움으로 ... – Havelock
php domdocument를 사용합니다 –