user3445862
user3445862

Reputation: 59

i want to remove <![CDATA[ ]]> in xml?

in xml file,i am displaying mysql data using php ..In that,i used <![CDATA[ data ]]> in that some content is coming .some empty tags are there like this "<![CDATA[ ]]>"; i want to remove that all how?

can you please tell me that one.

enter image description here

Upvotes: 0

Views: 3025

Answers (1)

dieBeiden
dieBeiden

Reputation: 178

If you just wish to remove the empty ones, you may use a regular expression replace. See this code for an example.

$xml = "<tags>
  <tag><![CDATA[here is content]]></tag>
  <tag><![CDATA[     ]]></tag>
</tags>";
$cleanedXml = preg_replace('/<!\[CDATA\[\s*\]\]>/ims', '', $xml);

The output then would be:

<tags>
  <tag><![CDATA[here is content]]></tag>
  <tag></tag>
</tags>

Hope this helps!

Upvotes: 2

Related Questions