我正試圖實現這一點。我有很多看起來像這樣的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/
這只是路徑的開始?
先謝謝你了,丹尼爾!
最大的可能。在正則表達式的幫助下... – Havelock
使用php domdocument –