简体   繁体   中英

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

I have an xml template that currently looks like this:

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
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

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):

$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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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