For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > June 2005 > Strip CDATA with regex









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Strip CDATA with regex
Johnny Elvers

2005-06-09, 8:56 am

Hi,

Can sombody here please help me a bit with a regular expression.
I have a xml file where I need to strip the CDATA sections of any
contained data.

Eg.
<xml>
<tag><[CDATA[ some data ]]></tag>
<tag><[CDATA[ some more data ]]></tag>
</xml>

Should end up like this:
<xml>
<tag><[CDATA[]]></tag>
<tag><[CDATA[]]></tag>
</xml>

Now, I have the start and end of the range
(\[CDATA\[)
and
(\]\]> )

But I cannot figure out how I match any character that is not like the
end of the range.

That is > is ok, ] is ok
but ]]> is not ok.

Thanks in advance,
Balaras
Janwillem Borleffs

2005-06-09, 8:55 pm

Johnny Elvers wrote:
> Now, I have the start and end of the range
> (\[CDATA\[)
> and
> (\]\]> )
>
> But I cannot figure out how I match any character that is not like the
> end of the range.
>


$stripped = preg_replace(
"/<\[CDATA\[.*\]\]>/",
"<[CDATA[]]>",
$xml
);


JW



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com