PHP: How to get rid of all <![CDATA[ and ]]> occurrences in a string?
I tried this as a test开发者_JAVA百科:
<?php
$crap = "<![CDATA[Hello, world!]]>";
$crap = str_replace(list("<![CDATA[", "]]>"), "", $crap);
echo $crap;
?>
But it returned this:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ')' in /srv/www/htdocs/test.php on line 3
Replace list
with array
. list
is used for making several variable attributions at the same time.
But you should not parse XML with str_replace
. Consider the following valid file:
<?xml version="1.0" ?>
<root>
<![CDATA[&]]>
</root>
After your replacement, it becomes:
<?xml version="1.0" ?>
<root>
&
</root>
which is invalid XML.
精彩评论