简体   繁体   English

PHP:如何摆脱所有 <![CDATA[ and ]]> 在字符串中出现?

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

I tried this as a test: 我试过这个测试:

<?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 . array替换list list is used for making several variable attributions at the same time. list用于同时生成多个变量属性。

But you should not parse XML with str_replace . 但是你不应该用str_replace解析XML。 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. 这是无效的XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM