Php 如何删除XML字符串中的元素?

Php 如何删除XML字符串中的元素?,php,xml,regex,Php,Xml,Regex,如何删除此XML字符串中的元素 $input= '<items>dfd jdh flkdjf <title>My Test Title </title>.... <store>my store 1</store> </items>.....'; $output= '<items>dfd jdh flkdjf .... <store>my store 1</s

如何删除此XML字符串中的
元素

$input=
'<items>dfd jdh flkdjf 
<title>My Test Title
</title>....
<store>my store 1</store>
</items>.....';

$output=
    '<items>dfd jdh flkdjf 
        ....
    <store>my store 1</store>
    </items>.....';
$input=
'dfd jdh flkdjf
我的考试题目
....
我的商店1
.....';
$output=
'dfd jdh flkdjf
....
我的商店1
.....';

谢谢

如果您正在处理未知的输入数据或生产代码,您应该使用XML解析器

如果您在测试环境中工作,并且输入数据已知:


$output=preg_replace('%[^如果您正在处理未知的输入数据或生产代码,您应该使用XML解析器

如果您在测试环境中工作,并且输入数据已知:

$output=preg_replace('%[^Simplexml

$str = '<items>1<title>lalala</title><others>...</others></items>';
$xml = simplexml_load_string($str);
unset($xml->children()->title);
$output = str_replace("<?xml version=\"1.0\"?>\n", '', $xml->asXml());
$str='1lalala…';
$xml=simplexml\u load\u字符串($str);
取消设置($xml->children()->title);
$output=str_replace(“\n”,”$xml->asXml());
Simplexml

$str = '<items>1<title>lalala</title><others>...</others></items>';
$xml = simplexml_load_string($str);
unset($xml->children()->title);
$output = str_replace("<?xml version=\"1.0\"?>\n", '', $xml->asXml());
$str='1lalala…';
$xml=simplexml\u load\u字符串($str);
取消设置($xml->children()->title);
$output=str_replace(“\n”,”$xml->asXml());

ok,您是如何使用xmldocument的?它是用于单元测试而不是用于生产的,所以正则表达式可以很好also@BoltClock:这取决于XML文档。好的,您如何使用xmldocument?它用于单元测试,不用于生产,所以正则表达式可以很好also@BoltClock:这取决于XML文档。谢谢,但标题是否有属性?更新。Ple请注意注意注意注意事项。谢谢,但是如果标题有atributes?更新。请注意注意注意注意事项。SimpleXML符合它的名称,+1Thanks,在实际情况下不起作用,因为标题元素在其他位置。我只给出简单的示例是可以修复的,但需要您用实际的xml再次更新您的问题…对于多个节点,您需要循环到ea中ch node…SimpleXML符合其名称+1 hanks,不适用于实际情况,因为title元素位于其他位置。我只给出了一个简单的示例,它是可修复的,但需要您用实际的xml再次更新您的问题…对于多个节点,您需要循环到每个节点。。。