1

Actual I have one problem. I want to append something to string in CDATA format in a XML file.

<url><![CDATA[http://www.kinguin.net/category/1356/l-a-noire-steam-key/?nosalesbooster=1&country_store=1&currency=EUR]]</url>

After I modify the string with this code:

$doc = simplexml_import_dom($dom);
    $items = $doc->xpath('//url');
    foreach ($items as $item) {
        $node = dom_import_simplexml($item);
        $node->textContent .= 'appendIT';
    }

I get:

<url>http://www.kinguin.net/category/1356/l-a-noire-steam-key/?nosalesbooster=1&amp;country_store=1&amp;currency=EURappendIT]]</url>

But how can I say "Put it back into the CDATA format"?

And in addition. I want to send this url to my database. But without the &amp; char. So how can I get the url without the &amp;?

Greetings and Thank You!

Jan
  • 277
  • 1
  • 6
  • 16
  • Possible duplicate of [Modify !\[CDATA\[\]\] in PHP? (XML)](http://stackoverflow.com/questions/42354598/modify-cdata-in-php-xml) – ThW Feb 25 '17 at 21:50

1 Answers1

0

As CDATA in XML stands for "character data", you can treat it as a string without parsing it. CDATA ends with ]] (assume not nested CDATA), so

$xmlstrIn = '<url><![CDATA[http://www.kinguin.net/category/1356/l-a-noire-steam-key/?nosalesbooster=1&country_store=1&currency=EUR]]</url>';
$xmlstrOut = preg_replace('|(\]\])|', 'appendIT$1', $xmlstrIn);

and send $xmlstrOut to your database.

Hope this solves the problem (unless you are doing research of SimpleXML extension or something).

Regards,

Nick