gpt4 book ai didi

php - 如何从 php ://input 中删除 XML 版本

转载 作者:行者123 更新时间:2023-12-04 02:15:00 28 4
gpt4 key购买 nike

我们有一个 PHP 脚本可以接收以下格式的 XML 数据:

<?xml version="1.0" encoding="utf-8"?>

<job>
<job_reference>123</job_reference>
<job_description>Lorem ipsum.</job_description>
<job_title>IT Manager</job_title>
etc...
</job>

脚本当前将此写入文件,替换现有数据。但是我们现在需要它附加到现有数据,但显然我们只能出现一次 <?xml version="1.0" encoding="utf-8"?>。否则 XML 文件将无效。我知道我需要将 fopen 模式从“w”更改为“a”,但我如何从 $xml 中删除 XML 版本行? .目前的代码是:

<?php
$xml = file_get_contents('php://input');
$data = $xml;
$file = fopen( 'broadbeantesttest.xml', "w") or exit("Unable to open file!");
// Write $somecontent to our opened file.
if (fwrite($file, $xml) === FALSE) {
echo "Cannot write to file ($file)";
exit;
}
fclose($file);
?>

最佳答案

你可以用 str_replace() 删除它:

$xmlString = file_get_contents('php://input');
$xmlString = str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $xmlString);
file_put_contents("broadbeantesttest.xml", $xmlString, FILE_APPEND);

关于php - 如何从 php ://input 中删除 XML 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34874038/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com