2024 年日本 PHP 研討會

DOMElement::setAttribute

(PHP 5, PHP 7, PHP 8)

DOMElement::setAttribute新增或修改現有屬性

說明

public DOMElement::setAttribute(字串 $qualifiedName, 字串 $value): DOMAttr|布林值

設定名為 qualifiedName 的屬性值。如果屬性不存在,則會建立它。

參數

qualifiedName

屬性的名稱。

value

屬性的值。

回傳值

已建立或修改的 DOMAttrfalse(如果發生錯誤)。

錯誤/例外

DOM_NO_MODIFICATION_ALLOWED_ERR

如果節點是唯讀的,則會引發此錯誤。

範例

範例 #1 設定屬性

<?php
$doc
= new DOMDocument("1.0");
$node = $doc->createElement("para");
$newnode = $doc->appendChild($node);
$newnode->setAttribute("align", "left");
?>

參見

新增註記

使用者貢獻的註記 6 個註記

Rakesh Verma - rakeshnsony at gmail dot com
14 年前
<?php
//將您的 HTML 儲存到 $html 變數中。
$html="
<html>
<head>
<title>未命名的文件</title>
</head>

<body>
<a href='http://example.com'>範例</a><br>
<a href='http://google.com'>Google</a><br>

<a href='http://www.yahoo.com'>Yahoo</a><br>
</body>

</html>"
;

$dom = new DOMDocument();
$dom->loadHTML($html);

//評估 HTML 中的錨點標籤
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for (
$i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');

//移除並設定 target 屬性
$href->removeAttribute('target');
$href->setAttribute("target", "_blank");

$newURL=$url."/newurl";

//移除並設定 href 屬性
$href->removeAttribute('href');
$href->setAttribute("href", $newURL);
}

// 儲存 html
$html=$dom->saveHTML();

echo
$html;

?>
lehal2@hotmail
11 年前
使用 DOM 先移除再將寬度和高度添加到文字中的第一個 img 標籤。我希望它可以幫助您節省時間。
<?php
$html
= '

<img src="http://www.example.com/images/header.jpg" width="898" height="223" style="border-bottom:5px solid #cccccc;"/>
<img src="http://www.example.com/images/header2.jpg" width="898" height="223" style="border-bottom:5px solid #cccccc;"/>
'
;

$doc = DOMDocument::loadHTML($html);
$c =0;
foreach(
$doc->getElementsByTagName('img') as $image){
if (
$c>0) continue;
foreach(array(
'width', 'height') as $attribute_to_remove){
echo
$attribute_to_remove;
if(
$image->hasAttribute($attribute_to_remove)){
$image->removeAttribute($attribute_to_remove);

}
if(
$attribute_to_remove=='height'){
if(!
$image->hasAttribute($attribute_to_remove)){
$image->setAttribute($attribute_to_remove,'220');

}}
if(
$attribute_to_remove=='width'){
if(!
$image->hasAttribute($attribute_to_remove)){
$image->setAttribute($attribute_to_remove,'700');

}}
$c = $c+1;
}
}
echo
$doc->saveHTML();
info at ensostudio dot ru
3 年前
使用/不使用值的屬性來渲染 HTML 5 標籤的解決方案
<?php
$dom
= new DOMImplementation();
$doc = $dom->createDocument(null, 'html', $dom->createDocumentType('html'));

$tag = $doc->appendChild($doc->createElement('input'));
$tag->setAttribute('type', 'text');
$tag->setAttribute('disabled', '');

echo
$doc->saveHTML($tag); // <input type="text" disabled="">

$doc->normalize(); // 標準化屬性
echo $doc->saveHTML($tag); // <input type="text" disabled>
?>
address at gmail dot com
16 年前
如果想要設定一個 id 為 "1" 的元素的屬性

<?php
$dom
= new DomDocument();
$dom->load('test.xml');
$xp = new DomXPath($dom);
$res = $xp->query("//*[@id = '1']");
$res->item(0)->setAttribute('title','2');
$dom->save('test.xml');
?>
karvjorm at users.sourceforge.net
17 年前
$dom = new DomDocument('1.0','iso-8859-15');

$ht_ml = $dom->appendChild($dom->createElement('html'));
$ht_ml->setAttribute('xmlns','http://www.w3.org/1999/xhtml');
$ht_ml->setAttribute('xml:lang','fi');
$ht_ml->setAttribute('lang','fi');

結果

<?xml version="1.0" encoding="iso-8859-15"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi" lang="fi">
Vasil Rangelov
17 年前
@karvjorm,
使用這種方法並非良好的實務作法。實際上,我認為這可能是一個 bug。
以「xml」開頭的名稱是保留的(在這種情況下是用於命名空間)。
我認為在這種情況下 setAttribute() 應該回傳 false,但我猜它並沒有。

儘管如此,正確的做法是使用 createElementNS()。
它允許您在建立元素節點時指定命名空間。

因此,與您的程式碼等效的做法(已加入 $html 以允許自動換行)是

$dom = new DomDocument('1.0','iso-8859-15');
$html = $dom->createElementNS('http://www.w3.org/1999/xhtml', 'html');
$ht_ml = $dom->appendChild($html);
$ht_ml->setAttribute('xml:lang','fi');
$ht_ml->setAttribute('lang','fi');
To Top