/ / Ako odstrániť posledných 10 znakov z XML súboru pomocou PHP a potom pridať nejaký text? - php, xml

ako odstrániť posledných 10 charecters v XML súbore pomocou PHP a potom pridať nejaký text? - php, xml

toto je môj PHP kód, ktorý berie dáta z formy:

<?php
$itemPath         = $_REQUEST["itemPath"];
$title          = $_REQUEST["title"];
$description1    = $_REQUEST["description1"];
$description2    = $_REQUEST["description2"];
$description3    = $_REQUEST["description3"];
$price          = $_REQUEST["price"];
$note            = $_REQUEST["note"];

$xmlfile = "women.xml";
$file = fopen($xmlfile,"r+");
$handle = "</items>";
$newphrase = $file;
//rewind($handle);
$newphrase = str_replace($handle, "", $newphrase);
fwrite($file, $newphrase);
fclose($file);

$file = fopen($xmlfile,"a+");
fwrite($file, "n<item path="women/".$itemPath.".jpg" type = "bitmapfile">n");
fwrite($file, "t<title>".$title."</title>n");
fwrite($file, "t<description1>".$description1."</description1>n");
fwrite($file, "t<description2>".$description2."</description2>n");
fwrite($file, "t<description3>".$description3."</description3>n");
fwrite($file, "t<description3>".$description3."</description3>n");
fwrite($file, "t<price>".$price."</price>n");
fwrite($file, "t<note>" . $note . "</note>n");
fwrite($file, "</item>n");
fwrite($file,"</items>");
fclose($file);
?>

a tu je XML:

<items id = "items">
<item path = "women/3.jpg"  type = "bitmapfile">
<title>No.3</title>
<description1>Flowers</description1>
<description2>50 ml</description2>
<description3>pure</description3>
<price>35</price>
<note></note>
</item>

<item path = "women/6.jpg" type = "bitmapfile">
<title>Trish McEvoy No.6</title>
<description1>Trish McEvoy No. 6 Mandarin & Ginger </description1>
<description2>50 ml / 1.7 fl oz Eau de Toilette</description2>
<description3>Authenticity: 100% Guaranteed</description3>
<price>35</price>
<note></note>
</item>

</items>

čo je s mojím PHP kódom zlé a ako by som mohol vymazať posledný a vložiť novú položku do XML súboru, potom pridať na koniec ???

mám použiť v PHP $ file = fopen ($ xmlfile, "a +"); ALEBO $ file = fopen ($ xmlfile, "r +");

odpovede:

0 pre odpoveď č. 1

Týmto spôsobom odstránite posledných 10 znakov ...

$xml = "...";
$xml = substr($xml, 0, strlen($xml)-10);

Ale toto nie je spôsob, ako povedal „fabrik“.


0 pre odpoveď č. 2
$newItem = "<item>test</item>";

echo str_replace("</items>", $newItem."</items>", file_get_contents($file));