使用php将一个xml文件中的标签值替换/替换为另一个xml [英] replace / substitute a tag value from one xml file to another xml using php
本文介绍了使用php将一个xml文件中的标签值替换/替换为另一个xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要将一个 xml 文件中的标签值替换/替换为另一个 xml
Need to replace / substitute a tag value from one xml file to another xml
我需要使用 php 将标签值替换为另一个 xml 文件
I need to replace the tag value to another xml file using php
文件 1
<complete>
<list>
<change>hello</change>
<manage>world</manage>
</list>
<list>
<change>hello1</change>
<manage>world1</manage>
</list>
<list>
<change>hello2</change>
<manage>world2</manage>
</list>
</complete>
文件 2
<complete>
<list>
<change>duplicate</change>
<manage>duplicate</manage>
</list>
<list>
<change>duplicate1</change>
<manage>duplicate1</manage>
</list>
<list>
<change>duplicate2</change>
<manage>duplicate2</manage>
</list>
</complete>
O/P
<complete>
<list>
<change>duplicate</change>
<manage>world</manage>
<change>duplicate1</change>
<manage>world1</manage>
<change>duplicate2</change>
<manage>world2</manage>
</list>
</complete>
我需要将标签管理从第一个 xml 文件替换为第二个 xml 文件.
I need the tag manage to the replaced from 1st xml file to 2nd xml file.
推荐答案
给你:
$file1 = file_get_contents("file1.xml");
$file2 = file_get_contents("file2.xml");
preg_match_all('%<manage>(.*?)</manage>%m', $file1, $match, PREG_PATTERN_ORDER);
$xmlmatch = $match[1][0];
$newxml = preg_replace('%<manage>.*?</manage>%m', "<manage>$xmlmatch</manage>", $file2);
file_put_contents("file2.xml", $newxml);
注意:确保 file2.xml
是可写的.
Note: Make sure file2.xml
is writable.
处理后的file2.xml
看起来像:
<complete>
<list>
<change>duplicate</change>
<manage>world</manage>
</list>
</complete>
这篇关于使用php将一个xml文件中的标签值替换/替换为另一个xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文