2024 年日本 PHP 研討會

DOMDocument::createEntityReference

(PHP 5, PHP 7, PHP 8)

DOMDocument::createEntityReference建立新的實體參考節點

說明

public DOMDocument::createEntityReference(字串 $name): DOMEntityReference|false

這個函式會建立 DOMEntityReference 類別的新實例。除非使用 (例如) DOMNode::appendChild() 插入,否則此節點不會顯示在文件中。

參數

name

實體參考的內容,例如實體參考扣除開頭的 & 和結尾的 ; 字元。

回傳值

新的 DOMEntityReference 或發生錯誤時回傳 false

錯誤/例外

DOM_INVALID_CHARACTER_ERR

如果 name 包含無效字元,則會引發此錯誤。

另請參閱

新增註釋

使用者貢獻的註釋 2 則註釋

alicewonder at shastaherps dot org
9 年前
看起來這個函式不適用於編號實體,僅適用於命名實體。

$nbspace = $dom->createEntityReference('nbsp');

可以正常運作

$nbspace = $dom->createEntityReference('#160');

無法正常運作。除非您修改 XSL 文件類型以包含您想要字元的命名實體,否則在產生 XSL 時,此函式相當無用。
Tuhin Bepari
11 年前
<?php
/*實體(Entity)是一組用於顯示特殊符號的單字。
例如,如果我們想要在 HTML 頁面中顯示版權符號,我們會使用 &copy; 程式碼,瀏覽器會將其轉換為實際的版權符號。
有很多實體,您可以在 http://dev.w3.org/html5/html-author/charref 找到它們。
如果您想在節點值中使用 < 或 > 或兩者 <>,則 XML 會發出警告或將此值設為節點。
因此,請告知 XML 解析器 < 或 > 不是標籤符號,而是一個實體。要做到這一點,您必須使用 &lt(<) 和 &gt;(>) 來代替 < 和 > 符號。

實體參考一律以 & 開頭,以 ; 結尾。
當您想在 DOMDocument::createEntityReference 中使用實體時,不需要使用 & 和 ; 符號作為開頭和結尾。請將它們移除。
然後將其附加到您想要顯示此符號的標籤。如下所示
*/
$dom=new DOMDocument("1.0","UTF-8");
$example=$dom->createElement("example","This is copyright ");
$entity=$dom->createEntityReference("copy");
$example->appendChild($entity);
$dom->appendChild($example);
echo
$dom->saveXML();

輸出為
This is copyright ©
To Top