tags:

views:

56

answers:

1

I have a xml file

<?xml version="1.0" encoding="UTF-8"?>
<text><![CDATA[<p><span style="font-family: arial black,avant garde; font-size: medium;"><strong>Extensible Markup Language</strong> (<strong>XML</strong>)</span> 
]]></text>

How i can replace <![CDATA[ and ]]> using php

A: 

A simple example:

$xml = file_get_contents('/path/to/file.xml');
$xml = str_replace('<![CDATA[', '', $xml);
$xml = str_replace(']]>', '', $xml);
echo $xml; // or whatever

You could do it all in one shot with preg_replace, but that's overkill in this scenario.

mway
Sorry for being harsh, but this is complete nonsense.
Tomalak
Then supply another answer instead of being completely unhelpful.
mway
@mway: I'm not being unhelpful, I'm waiting for a clarification from the OP. His question indicates that he is misconceiving something basic about XML, and I think it's better to find out what he's trying to achieve than giving him fundamentally flawed advice that does not actually solve his problem.
Tomalak
It's a pretty straightforward question with a straightforward answer. You have something that can be represented as a string, you want to remove parts of that string. It's not necessarily our prerogative to always contextually validate the question.
mway
That's the basic misconception. XML can be serialized into a string, but it *is not a string* and therefore cannot be manipulated with string functions without breaking it. You cannot simply string-replace bits of it you don't like, and in case of CDATA you don't even have to, unless you are doing it entirely wrong. I'm not seeking to exercise some alleged prerogative, I'm trying to solve the problem the OP has. His problem description is clouded by his limited knowledge about XML and therefore just doing what he asks for is the wrong approach.
Tomalak
Yes, in the given code, that would invalidate the XML. However he didn't say that anything was wrong, he just asked how to do something simple, so I gave him a simple answer to accomplish that goal. If he tries to use it with something that requires valid XML (like, say, _parsing it_), then clearly he will run into problems.
mway
Right. So when you think one step further — what would the remaining mess of a string be good for? You can't really say that this would buy him anything. :-) Unless he explains, this question cannot be answered with confidence, and that's why I wait. Just don't take my first comment personally, it was directed at the code, not at you. ;-)
Tomalak
Touche, and conceded. ;)
mway