To prevent a parser error when the comment string would contain the character sequence "--", do this:
<?php
$CommentString = 'This contains -- some weird -- characters.';
$CommentNode = $DomDocument->createComment(
str_replace('--', '-'.chr(194).chr(173).'-', $CommentString)
);
?>
This will insert a Soft Hyphen in between the two hyphens which will not cause the parser to error out.
DOMDocument::createComment
(PHP 5)
DOMDocument::createComment — 新しい comment ノードを作成する
説明
この関数は、DOMComment クラスの新しいインスタンスを作成します。このノードは、( DOMNode::appendChild() などで) 挿入されない限り、ドキュメント内にあらわれません。
パラメータ
-
data -
コメントの内容。
返り値
新しい DOMComment、あるいはエラーが発生した場合は
FALSE を返します。
参考
- DOMNode::appendChild() - 子要素群の最後に新しい子要素を追加する
- DOMDocument::createAttribute() - 新しい属性を作成する
- DOMDocument::createAttributeNS() - 関連付けられた名前空間に新しい属性を作成する
- DOMDocument::createCDATASection() - 新しい cdata ノードを作成する
- DOMDocument::createDocumentFragment() - 新しい文書片を作成する
- DOMDocument::createElement() - 新しい要素ノードを作成する
- DOMDocument::createElementNS() - 関連付けられた名前空間に新しい要素を作成する
- DOMDocument::createEntityReference() - 新しいエンティティ参照ノードを作成する
- DOMDocument::createProcessingInstruction() - 新しい PI ノードを作成する
- DOMDocument::createTextNode() - 新しいテキストノードを作成する
php dot sirlancelot at spamgourmet dot com
25-Nov-2009 09:10
capps at solareclipse dot net
30-Mar-2007 12:55
Use caution: this function does NOT escape, encode, or otherwise change the contents of the string.
This means that if your string contains two hyphens in a row (--), that will *end* the comment, which can easily create invalid XML when the document is serialized.
