简体   繁体   English

如何从文件第4行开始向文件添加内容?

[英]How to add content to a file, but start from row 4?

I have an xml template that currently looks like this: 我有一个当前看起来像这样的xml模板:

XML: XML:

<?xml version="1.0" encoding="UTF-8"?>
<tv>
<channel id="560"><display-name lang="he">560</display-name></channel>

**** The content I save later will start from line 4 (here) ****

</tv>

My PHP file is dragging content from other files: 我的PHP文件正在从其他文件中拖动内容:

<?php
file_get_contents("http://website.com/guide?1");
file_get_contents("http://website.com/guide?2");
file_get_contents("http://website.com/guide?3");
file_get_contents("http://website.com/guide?4");
file_get_contents("http://website.com/guide?5");

What I want to do is to add the content to the file, 我要做的就是将内容添加到文件中,

But start from line 4 & without deleting the last line 但是从第4行开始且不删除最后一行

UPDATE: 更新:

<tv>

//I want the content to stick to here, before the /tv root

</tv>  //Sould be the last line

<channel id="861"><display-name lang="he">861</display-name></channel>

<programme start="20180127013500 +0200" stop="20180127020000 +0200" channel="861">
<title lang="he">היום בלילה</title>
<desc lang="he">גורי אלפי מסכם מדי לילה את אירועי היום בתכנית בידור מצחיקה ובועטת: האנשים שעשו את החדשות, פוליטיקאים, סטנדאפיסטים ומוזיקאים. כ' סמויות.</desc>
</programme>

Assuming that your files are also XML compliant, you can do that (otherwise, try to load the remote files using the DOMDocument::loadHTMLFile method in place of DOMDocument::load method that is for XML only): 假设您的文件也符合XML,则可以这样做(否则,请尝试使用DOMDocument::loadHTMLFile方法代替仅用于XML的DOMDocument::load方法来加载远程文件):

$dom = new DOMDocument;
$dom->load('template.xml');
$root = $dom->documentElement;

for ($i=1; $i<6; $i++) {
    $domi = new DOMDocument;
    $domi->load('http://website.com/guide?'.$i);
    $rooti = $domi->documentElement;
    $node = $dom->importNode($rooti, true);
    $root->appendChild($node);
}

$result = $dom->saveXML();

Note that if your remote files are not loaded, you can set stream parameters (in particular the user agent) using libxml_set_streams_context . 请注意,如果未加载远程文件,则可以使用libxml_set_streams_context设置流参数(尤其是用户代理)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM