DOMDocument::createEntityReference

(PHP 5, PHP 7, PHP 8)

DOMDocument::createEntityReference新しいエンティティ参照ノードを作成する

説明

public DOMDocument::createEntityReference(string $name): DOMEntityReference|false

この関数は、DOMEntityReference クラスの新しいインスタンスを作成します。このノードは、( DOMNode::appendChild() などで) 挿入されない限り、ドキュメント内にあらわれません。

パラメータ

name

エンティティ参照の内容、つまり、エンティティ参照から 先頭の & および末尾の ; を取り除いたもの。

戻り値

新しい DOMEntityReference、 あるいはエラーが発生した場合には false を返します。

エラー / 例外

DOM_INVALID_CHARACTER_ERR

name が無効な文字を含んでいる場合に発生します。

参考

add a note add a note

User Contributed Notes 2 notes

up
3
alicewonder at shastaherps dot org
9 years ago
It appears that this does not work with numbered entities, only named entities.

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

works

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

does not. This makes this function rather useless when generating an XSL unless you modify the XSL doctype to include the named entity for the character you want.
up
-1
Tuhin Bepari
11 years ago
<?php
/*Entity is a group of words which print a special symbol.
Like if we want to show copy right symbol in html page then we use &copy; code and browser convert this to actual copyright symbol.
There have lots of entity, you can find them all form http://dev.w3.org/html5/html-author/charref
if you want to use < or > or both <> into a node value than xml will give and warning or make this value as a node.
So tell the xml parser that < or > is not tag symbol it is a entity.To do that you have to right &lt(<) and &gt;(>) instead of < and > symbol.

Entity references always begin with an ampersand (&) and end with a semicolon (;).
DO not need to use & and ; symbol begin and end of entity.Remove it when you want to use it to DOMDocument::createEntityReference
Then append to to a tag where you want to show this symbol.Like below
*/
$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();

output is
This is copyright ©
To Top