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.

$doc= new DOMDocument('1.0', "UTF-8");$invalid_elem = $doc->createElement('field', 'incorrect attribute');
$invalid_attr = $doc->createAttribute('name');
$invalid_attr->value = 'foo&bar';
$invalid_elem->appendChild($invalid_attr);
$doc->appendChild($invalid_elem);$valid_elem = $doc->createElement('field', 'correct attribute');
$valid_attr = $doc->createAttribute('name');
$valid_attr->value = 'foo&bar';
$valid_elem->appendChild($valid_attr);
$doc->appendChild($valid_elem);?>

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

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

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:

Lorem';

$dom = new \DOMDocument();
$dom->loadHTML($content);

$node = $dom->getElementsByTagName('h2')->item(0);
$node->setAttribute('data-custom','true');

print $dom->saveHTML($node);
// 

Lorem

Ngoài ra, vì nó được hình thành tốt, hãy coi nội dung là XML để tránh các thẻ HTML bổ sung được thêm vào:

Lorem';

$dom = new \DOMDocument();
$dom->loadXML($content);

$dom->documentElement->setAttribute('data-custom','true');

print $dom->saveXML($dom->documentElement);
// 

Lorem

Đã trả lời ngày 12 tháng 9 năm 2013 lúc 8:15Sep 12, 2013 at 8:15

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

(Php 5, Php 7, Php 8)

Domelement :: SetAttribute - Thêm mới hoặc sửa đổi thuộc tính hiện có — Adds new or modifies existing attributeAdds new or modifies existing attribute

Sự mô tả

Public Kmousement :: SetAttributDOMElement::setAttribute(string


Do more tomorrow. For less.

0, string

Do more tomorrow. For less.

1): DOMAttr|bool DOMElement::setAttribute(string

Do more tomorrow. For less.

0
, string

Do more tomorrow. For less.

1
): DOMAttr|bool

Thông số


Do more tomorrow. For less.

2

Tên của thuộc tính.


Do more tomorrow. For less.

3

Giá trị của thuộc tính.

Trả về giá trị

DOMATTR hoặc


Do more tomorrow. For less.

4 được tạo hoặc sửa đổi nếu xảy ra lỗi.DOMAttr or

Do more tomorrow. For less.

4 if an error occurred. DOMAttr or

Do more tomorrow. For less.

4
if an error occurred.

Errors/Exceptions


Do more tomorrow. For less.

6

Lớn lên nếu nút được đọc lại.

Ví dụ

Ví dụ #1 Cài đặt thuộc tính


Do more tomorrow. For less.

7

Xem thêm

  • Domelement :: HasAttribut () - Kiểm tra xem thuộc tính có tồn tại
  • Domelement :: getAttribut () - Trả về giá trị của thuộc tính
  • Domelement :: removeAttribut () - Xóa thuộc tính

Rakesh Verma - Rakeshnsony tại Gmail Dot Com ¶ ¶

11 năm trước


Do more tomorrow. For less.

8


Do more tomorrow. For less.

9

Lorem

0


Do more tomorrow. For less.

10


Do more tomorrow. For less.

11

Thông tin tại Ensostudio Dot Ru ¶ ¶

10 tháng trước


Do more tomorrow. For less.

12


Do more tomorrow. For less.

13


Do more tomorrow. For less.

14

Địa chỉ tại Gmail Dot Com ¶ ¶

14 năm trước


Do more tomorrow. For less.

15


Do more tomorrow. For less.

16


Do more tomorrow. For less.

17

[email protected] ¶ ¶

9 năm trước


Do more tomorrow. For less.

18


Do more tomorrow. For less.

19


Do more tomorrow. For less.

20

Vasil Rangelov ¶ ¶

15 năm trước


Do more tomorrow. For less.

21


Do more tomorrow. For less.

22


Do more tomorrow. For less.

23


Do more tomorrow. For less.

24


Do more tomorrow. For less.

17

Làm thế nào để bạn đặt các thuộc tính?

Phần tử.setAttribution () Đặt giá trị của một thuộc tính trên phần tử được chỉ định.Nếu thuộc tính đã tồn tại, giá trị được cập nhật;Mặt khác, một thuộc tính mới được thêm vào với tên và giá trị được chỉ định.Để có được giá trị hiện tại của một thuộc tính, hãy sử dụng getAttribution ();Để xóa một thuộc tính, hãy gọi RemoveAtribution (). Sets the value of an attribute on the specified element. If the attribute already exists, the value is updated; otherwise a new attribute is added with the specified name and value. To get the current value of an attribute, use getAttribute() ; to remove an attribute, call removeAttribute() . Sets the value of an attribute on the specified element. If the attribute already exists, the value is updated; otherwise a new attribute is added with the specified name and value. To get the current value of an attribute, use getAttribute() ; to remove an attribute, call removeAttribute() .

Làm thế nào để thêm thuộc tính vào phần tử HTML trong PHP?

Bạn có thể dễ dàng thêm thuộc tính vào thẻ HTML bằng Regex và PHP.Hàm preg_replace () tìm kiếm chuỗi khớp với mẫu (biểu thức chính quy) và thay thế bằng thay thế.Sử dụng hàm preg_replace () với biểu thức thông thường để thêm thuộc tính vào thẻ HTML bằng PHP.using Regex and PHP. The preg_replace() function searches string matches to the pattern (regular expression) and replaces with replacement. Use preg_replace() function with regular expression to add attribute to HTML tag using PHP.using Regex and PHP. The preg_replace() function searches string matches to the pattern (regular expression) and replaces with replacement. Use preg_replace() function with regular expression to add attribute to HTML tag using PHP.