开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜