I’m now using the function fwrite(); in PHP. But i want to locate my new things after a specific rule.
This wil be the output.
<?xml version="1.0" encoding="utf-8" ?>
<logs>
<log type="text">the new log</log>
<log type="text>the old log</log>
<log type="login">some other log.</log>
</logs>
How can i get the new log in the new log and not on the end. I only can find something like file_get_contents and then str_replace. But that seems really not efficient.
My php Code:
$file = $this->path.'logs.xml';
// Open our file. And Create file if it doesn't exsist
$fopen = fopen($file, "w+");
// Looks if file is empty.
if(filesize($file) == 0) {
/*
* Put your data in XML data.
*/
$xmlData = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> \r\n";
$xmlData .= "<logs> \r\n";
$xmlData .= "\t<log type=\"".$data[0]."\">\r\n";
$xmlData .= "\t\t<author>".$data[1]."</author>\r\n";
$xmlData .= "\t\t<action>".$data[2]."</action>\r\n";
$xmlData .= "\t\t<result>".$data[3]."</result>\r\n";
$xmlData .= "\t\t<note>".$data[4]."</note>\r\n";
$xmlData .- "\t</log>\r\n";
$xmlData .= "</logs>";
} else {
}
if(is_writeable($file)) {
fwrite($fopen, $xmlData);
return true;
}
return false;
fclose($fopen);
Sincerely thank you.
Well, you’re lucky your data is in XML. PHP has got a bunch of easy to use libraries (extensions) that deal with XML data. For example SimpleXML or the more capable DOM (both extension are enabled by default).