您可以使用XMLparser来更新文档的 URL:
// Initial string
$html = '<!doctype html>
<html>
<body>
<img src="computerIcon.png">
</body>
</html>
';
$proxy = 'https://proxy.example.com/?url=https://domain.example.com/';
// Load HTML
$xml = new DOMDocument("1.0", "utf-8");
$xml->loadHTML($html);
// for each <img> tag,
foreach($xml->getElementsByTagName('img') as $item) {
// update attribute 'src'
$item->setAttribute('src', $proxy . $item->getAttribute('src'));
}
$xml->formatOutput = true;
echo $xml->saveHTML();
输出:
<!DOCTYPE html>
<html><body>
<img src="https://proxy.example.com/?url=https://domain.example.com/computerIcon.png">
</body></html>
演示:https ://3v4l.org/bW68Z