开发者

PHP: How to get rid of all <![CDATA[ and ]]> occurrences in a string?

开发者 https://www.devze.com 2023-01-01 23:55 出处:网络
I tried this as a test开发者_JAVA百科: <?php $crap = \"<![CDATA[Hello, world!]]>\"; $crap = str_replace(list(\"<![CDATA[\", \"]]>\"), \"\", $crap);

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

精彩评论

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

关注公众号