Tôi đang cố gắng phân tích cú pháp tài liệu và nhận tất cả các thẻ hình ảnh và thay đổi nguồn cho một cái gì đó khác.php DomDocument thêm thẻ phụ
$domDocument = new DOMDocument();
$domDocument->loadHTML($text);
$imageNodeList = $domDocument->getElementsByTagName('img');
foreach ($imageNodeList as $Image) {
$Image->setAttribute('src', 'lalala');
$domDocument->saveHTML($Image);
}
$text = $domDocument->saveHTML();
Các $ text ban đầu trông như thế này:
<p>Hi, this is a test, here is an image<img src="http://mysite.com/beer.jpg" width="60" height="95" /> Because I like Beer!</p>
và đây là sản phẩm $ text:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>Hi, this is a test, here is an image<img src="lalala" width="68" height="95"> Because I like Beer!</p></body></html>
Tôi nhận được một loạt các thẻ phụ (html, cơ thể và nhận xét ở trên cùng) mà tôi không thực sự cần. Bất kỳ cách nào để thiết lập DOMDocument để tránh thêm các thẻ phụ này?
Cảm ơn bạn!
cần đọc: $ text = preg_replace ('/^ /', '', str_replace (mảng ('', '', '
', ''), mảng ('', '', ' ',' '), $ domDocument-> saveHTML())); –'preg_replace', thực sự? – sglessard