I need to search and replace in multiple xml files. I am looking to write a script to do this job as i have many files to edit. For example,
<search position="after" index="0,1,3" offset="2">
<![CDATA[
<?php -- code to be searched -- ?>
]]>
</search>
<add>
<![CDATA[ --code to add here-- ]]>
</add>
I need to change above code to following
<search index="1,2,4">
<![CDATA[
<?php -- code to be searched -- ?>
]]>
</search>
<add position="after" offset="2">
<![CDATA[ --code to add here-- ]]>
</add>
I need to
1. move position from search and add tag, position can be 'replace','after','before'.
2. index tag should also be moved to add tag, and incremented as new index starts from 1 instead of zero
3. move offset to add tag
As far as I know Searching for a particular string and replacing is easy using str_replace
but i cannot get how to search for a string and replace or append next line and iterating it in all files ending with .xml extension. What is the best way to do it either in php or command line?