如何从包含双引号''“的字符串特定单词中删除?

时间:2013-03-23 15:51:20

标签: php double-quotes

我想从我的字符串中删除<?xml version="1.0" encoding="utf-8"?>包含此内容以及其他很多内容。如何删除它因为几个双引号我有问题来定义str_replace函数。

4 个答案:

答案 0 :(得分:1)

然后使用单引号然后使用str_replace:

$yourstring = str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $yourstring);

答案 1 :(得分:1)

您可以考虑使用单引号:

str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $myString);

答案 2 :(得分:1)

如果您希望使用双引号来定义字符串,那么您需要\转义它们:

str_replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "", $myString);
//                         ^    ^           ^      ^

答案 3 :(得分:0)

以下是解决方案:

<?php

    $xml = '<?xml version="1.0" encoding="utf-8"?>
            <parentnode>
                <childnode>
                    Hello World!
                </childnode>
            </parentnode>';

    $xml_to_delete = '<?xml version="1.0" encoding="utf-8"?>';

    $new_xml = str_replace($xml_to_delete, "", $xml);

    echo $new_xml; //Raw XML.

    echo "<br/>";

    echo htmlentities($new_xml); //For seeing output in browsers, instead of XML rendered.