I am trying to change the data in a .xml document. I am pulling contact infomation from a database and adding it to my phone system's speed dial list. I got everything else to work except this...
My code looks like:
$fileurl = 'data2.xml';
$rc = (starts with 300 and adds 1 with every loop)
$misc = (11 digit phone number with no spaces (ex 14025551212))
$fullname = (first and last name of customer)
$search = $rc."</c1> \n <c2 /> \n <c3 />";
$data = $rc."</c1> \n <c2>9".$misc."</c2> \n <c3>".$fullname."</c3> ";
$file_contents = file_get_contents($fileurl);
$fh = fopen($fileurl, "w");
$file_contents = str_replace($search,$data,$file_contents);
fwrite($fh, $file_contents);
fclose($fh);
The Data in the data2.xml file looks like this:
- <item row="100">
<c0>100</c0>
<c1>*300</c1>
<c2 />
<c3 />
</item>
- <item row="101">
<c0>101</c0>
<c1>*301</c1>
<c2 />
<c3 />
</item>
...ect with rows going up to 999
And I need it changed to look like this:
- <item row="100">
<c0>100</c0>
<c1>*300</c1>
<c2>14025551212</c2>
<c3>John Smith</c3>
</item>
- <item row="101">
<c0>101</c0>
<c1>*301</c1>
<c2>14025552121</c2>
<c3>Jane Smith</c3>
</item>
...ect with rows going up to 999
I am looking at replacing what I searched for ($search) and change it to the data I get from my database ($data). It seems like when I search for <c2 /> it errors like there is something stopping it. The problem is the format of the data.xml file, but I can not change it. If I leave off the <c2 /> and <c3 /> the code works fine but looks like this:
- <item row="100">
<c0>100</c0>
<c1>*300</c1>
<c2>14025551212</c2>
<c3>John Smith</c3>
<c2 />
<c3 />
</item>
I got to get rid of the <c2 /> and <c3 /> and replace it with my data. Also eventually I will have less contacts and need to revert the data back to the original format. I have tried everything I know and no success. Anyone got any ideas that would work? I am thinking its something simple that I am overlooking.
Thanks!