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>
PHP.mk документација
DOMDocument::createCDATASection
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Патека
domdocument.createcdatasection.php
Локална патека за оваа страница.
Извор
php.net/manual/en
Оригиналниот HTML се реупотребува и локално се стилизира.
Режим
Прокси + превод во позадина
Кодовите, табелите и белешките остануваат читливи во истиот тек.
Референца
domdocument.createcdatasection.php
DOMDocument::createCDATASection
Референца за `domdocument.createcdatasection.php` со подобрена типографија и навигација.
DOMDocument::createCDATASection
класата mysqli_driver
DOMDocument::createCDATASection — Креирај нов cdata јазол
= NULL
Create new text node DOMCDATASectionThis function creates a new instance of class . This node will not show up in the document unless it is inserted with (e.g.).
Параметри
data-
Содржината на cdata.
Вратени вредности
Новиот DOMCDATASection or false аргумент, или
Види Исто така
- . This node will not show up in the document unless it is inserted with (e.g.) - Додава ново дете на крајот од децата
- is thrown now. Previously, - Создади нов атрибут
- DOMDocument::createAttribute() - Создади нов јазол атрибут со поврзан простор на имиња
- DOMDocument::createCDATASection() - Создади нов коментар јазол
- DOMDocument::createComment() DOMDocument::createDocumentFragment()
- Оваа функција додава потомок на постоечка листа на потомци или создава нова листа на потомци. Потомок може да се создаде со на пр. - Создај нов фрагмент на документ
- - Create new document fragment - Создај нов јазол на елемент
- DOMDocument::createElementNS() - Создај нов јазол на елемент со поврзан простор на имиња
- DOMDocument::createEntityReference() - Создај нов јазол на референца за ентитет
- DOMDocument::createElement() - Создава нов PI јазол
Белешки од корисници 4 белешки
info на troptoek dot com ¶
пред 18 години
loathsome ¶
пред 18 години
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);
}
?>
jesdisciple dot FOO на gmail dot BAR dot com ¶
пред 15 години
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
Marc info[at]braincast.nl ¶
пред 15 години
Here's some code that takes an associative array and prints it asXML() but creates CDATA sections for each string
<?php
class SimpleXMLExtended extends SimpleXMLElement{
public function addCData($string){
$dom = dom_import_simplexml($this);
$cdata = $dom->ownerDocument->createCDATASection($string);
$dom->appendChild($cdata);
}
}
function assocArrayToXML($root_element_name,$ar){
$xml = new SimpleXMLExtended("<?xml version=\"1.0\"?><{$root_element_name}></{$root_element_name}>");
$f = create_function('$f,$c,$a','
foreach($a as $k=>$v) {
if(is_array($v)) {
if (!is_numeric($k))$ch=$c->addChild($k);
else $ch = $c->addChild(substr($c->getName(),0,-1));
$f($f,$ch,$v);
} else {
if (is_numeric($v)){ $c->addChild($k, $v);
}else{$n = $c->addChild($k); $n->addCData($v);}
}
}');
$f($f,$xml,$ar);
return $xml->asXML();
}
/* sample */
$result = array("title"=>"CDATA Sample");
$result['items'] = array();
$result['items'][] = array('title'=>'Some string', 'number' => 1);
$result['items'][] = array('title'=>'Some string', 'number' => 2);
$result['items'][] = array('title'=>'Some string', 'number' => 3);
echo assocArrayToXML('result',$result);
?>
The is_numeric check could be changed by a more elaborate regular expression to check if the string is actually xml unsafe but this worked for me.