使用 PHP 写入文件
基本上我想做的是使用 PHP 打开一个 xml 文件并使用 php 编辑它,现在我可以使用 fopen() 函数来做到这一点。 但我的问题是我想将文本附加到文档的中间。假设 xml 文件有 10 行,我想在最后一行 (10) 之前附加一些内容,所以现在将是 11 行。这可能吗?谢谢
Bassicly what I want to do is using PHP open a xml file and edit it using php now this I can do using fopen() function.
Yet my issue it that i want to append text to the middle of the document. So lets say the xml file has 10 lines and I want to append something before the last line (10) so now it will be 11 lines. Is this possible. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据该文件的大小,您可能会这样做:
不过,您之后仍需要重新验证 XML 语法...
Depending on how large that file is, you might do:
Still, you'll need to revalidate XML-syntax afterwards...
如果您希望能够从中间修改文件,请使用
c+
打开模式:上面将在第五行写入“foo”,而不必完全读取文件。
但是,如果您要修改 XML 文档,那么使用 DOM 解析器可能会更好:
当然,上面的代码取决于实际的 XML 文档结构。但是,
$element->getLineNo()
是这里的关键。If you want to be able to modify a file from the middle, use the
c+
open mode:The above will write "foo" on the fifth line, without having to read the file entirely.
However, if you are modifying a XML document, it's probably better to use a DOM parser:
Of course, the above code depends on the actual XML document structure. But, the
$element->getLineNo()
is the key here.