PHP Doku:: Create new cdata node - domdocument.createcdatasection.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzXML-ManipulationDocument Object ModelThe DOMDocument classDOMDocument::createCDATASection

Ein Service von Reinhard Neidl - Webprogrammierung.

The DOMDocument class

<<DOMDocument::createAttributeNS

DOMDocument::createComment>>

DOMDocument::createCDATASection

(PHP 5)

DOMDocument::createCDATASectionCreate new cdata node

Beschreibung

DOMCDATASection DOMDocument::createCDATASection ( string $data )

This function creates a new instance of class DOMCDATASection. Dieser Knoten wird in Ihrem Dokument nicht sichtbar sein, bis dieser zum Beispiel mit der Funktion DOMNode->appendChild() eingefügt wird.

Parameter-Liste

data

The content of the cdata.

Rückgabewerte

The new DOMCDATASection or FALSE if an error occured.

Siehe auch


3 BenutzerBeiträge:
- Beiträge aktualisieren...
jesdisciple dot FOO at gmail dot BAR dot com
8.07.2010 13:44
If you would like to refer to the documentation for the class of the returned object, see http://www.php.net/manual/en/class.domcharacterdata.php
info at troptoek dot com
25.03.2008 6:40
A common issue seems to be adding javascript to CDATA and the browser throwing a javascript error. To ensure the javascript works use the following code when adding CDATA:

<?php
/**
 * Append Caracter Data to a node and check for a javascript node
 *
 * @param DOMElement $appendToNode
 * @param string $text
 */
function appendCdata($appendToNode, $text)
{
    if (
strtolower($appendToNode->nodeName) == 'script') {  // Javascript hack
       
$cm = $appendToNode->ownerDocument->createTextNode("\n//");
       
$ct = $appendToNode->ownerDocument->createCDATASection("\n" . $text . "\n//");
       
$appendToNode->appendChild($cm);
       
$appendToNode->appendChild($ct);
    } else { 
// Normal CDATA node
       
$ct = $appendToNode->ownerDocument->createCDATASection($text);
       
$appendToNode->appendChild($ct);
    }
}
?>
The result should be:

<script type="text/javascript">
//<![CDATA[
function someJsText() {
   document.write('Some js with <a href="#">HTML</a> content');
}
//]]></script>
loathsome
4.08.2007 23:28
Here's a function that will create a CDATA-section around a string coming from SimpleXML.

<?php
function sxml_cdata($path, $string){
 
$dom = dom_import_simplexml($path);
 
$cdata = $dom->ownerDocument->createCDATASection($string);
 
$dom->appendChild($cdata);
}
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)