Hướng dẫn php domdocument add attribute - thuộc tính thêm php domdocument

This may be obvious to most people, but it gave me a few minutes of head-scratching before I figured it out, but ampersands need to be properly escaped as HTML entities.



Will output:

incorrect attribute
correct attribute

As near as I can figure out, no exception is thrown for an invalid attribute. This will affect DOMDocuments created as HTML as well as XML.

Tôi muốn tạo chức năng thêm một số thuộc tính vào thẻ gốc của HTML đã cho.

Tôi đang làm điều này:

    $dom = new \DOMDocument[];
    $dom->loadHTML[$content];

    $root = $dom->documentElement;

    $root->setAttribute["data-custom","true"];

Và cho $content='

Lorem

'

Nó trở lại:


Do more tomorrow. For less.

Trong khi nên chỉ là:

Lorem

Cách tạo DomDocument không tạo doctype, html, thẻ cơ thể, mà chỉ hoạt động trên HTML đã cho và cách chọn nút gốc của HTML đã cho

PS. Tôi sẽ không bao giờ sử dụng Regex để quản lý HTML.

Hỏi ngày 12 tháng 9 năm 2013 lúc 7:29Sep 12, 2013 at 7:29

Adam Pietrasiakadam PietrasiakAdam Pietrasiak

12.3k7 Huy hiệu vàng74 Huy hiệu bạc91 Huy hiệu Đồng7 gold badges74 silver badges91 bronze badges

1

Khi bạn xuất HTML, chọn một nút cụ thể thay vì toàn bộ tài liệu:

Bài Viết Liên Quan

Chủ Đề