FarbodKain FarbodKain - 6 months ago 3
PHP Question

create xml from other xml files by php

i have 2 xml files look likes below:

<hotel_name>hotel X</hotel_name>
<title>Room X</title>
<title>Room Y</title>
<PriceA> 400$</PriceA>

i want to combine these files to look like these:

<xml file 1>
<item name="Hotel X">
<item name="Room X" price="300$" />
<item name="Room Y" price="400$" />
</xml file 1>

any advice really appreciated.


The easiest way for beginner would be to read the file one by one and evaluate every lines with that function strpos:


Another way is to use regular expressions like this:

$destination = preg_replace(
        '<item name="$1" price="$2" />',
        "<xml file $1>\n<item name=\"$2\">$3\t<details>$4</details>$5</item>\n</xml file $1>"

I think with that I manage to preserve pretty much the initial idea (regarding your short example).

enter image description here

Here's my whole coding I used for test :


If you have question, write a comment and I will answer you tomorrow. If everything works pretty much as you wanted, then accept my answer.