2016-05-12 27 views
1

Ich verwende php, um etwas in eine Datei einzufügen. Ich will das, was ich einfügen möchte in die Datei nach 5 Zeilen von oben eingefügt werden.PHP in Datei einfügen

Wie unten ist ein .txt Dateibeispiel.

Dies ist der 3. Zeile

Dies ist der 4. Zeile

Dies ist 5.Leine

Here should be my content inserted with PHP 
Here is my previous content inserted with PHP 

Dies ist 1. Zeile

Diese

2. Zeile ist diese ist ein anderer Text in der Datei .

Nun kann ich einfügen, aber nicht nach Zeilen, die ich will. Ich verwende unter PHP-Code einzufügen: -

<?php 
$file = ($_SERVER['DOCUMENT_ROOT'] . '/sitemap.txt');; 
$current = file_get_contents($file); 
$current .= "http://www.onlinedealsindia.in/deal/SOME-UNIQUE-LINK\n"; 
file_put_contents($file, $current); 
?> 
+0

prüfen http://stackoverflow.com/questions/5106719/trying-to-insert-text-into-a-file-above-a-certain-line – Saty

+0

Nicht in der Lage zu verstehen. Brauchen Sie einen guten und einfachen Code. –

+0

Wie wäre es mit '$ contents = str_replace ($ contents, $ fifthline_text, $ fiftyline_text. PHP_EOL. $ Content_to_insert)'? –

Antwort

2

Versuchen Sie folgendes:

<?php 
    $test = "http://www.onlinedealsindia.in/deal/SOME-UNIQUE-LINK"; 
    $file = ($_SERVER['DOCUMENT_ROOT'] . '/sitemap.txt'); 
    $contents = explode("\n", file_get_contents($file), 6); 
    file_put_contents($file, $contents[0]."\n".$contents[1]."\n".$contents[2]."\n".$contents[3]."\n".$contents[4]."\n".$test."\n".$contents[5]); 
?> 
0

Sie können Datei verwenden, um die Datei als eine Anordnung von Linien zu bekommen, dann ändern Sie die Zeile, die Sie brauchen, und Umschreiben die ganzen Daten zurück in die Datei.

<?php 
$file = ($_SERVER['DOCUMENT_ROOT'] . '/sitemap.txt');; 
$current = file_get_contents($file); 
$current .= "http://www.onlinedealsindia.in/deal/SOME-UNIQUE-LINK\n"; 

$line_i_need_to_change = 5-1; // Since line array starts from 0 

$lines = file($file , FILE_IGNORE_NEW_LINES); 

$lines[$line_i_need_to_change] = $current.$lines[$line_i_need_to_change]; 

//I noticed that $current have \n that helps to move the old data to next line 

file_put_contents($file , implode("\n", $lines)); 

?> 
0

SKIP_LINES sagt, wie viele Zeilen Sie überspringen möchten. Der neue Inhalt, den Sie hinzufügen möchten, sollte ein Array sein, damit es richtig gehandhabt wird. Die Funktion file() erledigt die Aufgabe.

OLD:

<?php 
define('SKIP_LINES', 5); 
$file = 'existing.txt'; 

// read existing file into array 
$existing = file($file); 

// fill new array with existing data to SKIP_LINES 
$new = array(); 
for ($i = 1; $i <= SKIP_LINES; $i++) 
    $new[] = array_shift($existing); 

// this is the new content that you want to add (as array)                 
$addThis = file("newcontent.txt"); 

// merge skipped 5 lines, new content, and the rest of the existing file 
$new = array_merge($new, $addThis, $existing); 

// write the new file 
file_put_contents($file, $new); 

Edit:

NEW Version auf Ihr Dingen angepasst.

<?php 
define('SKIP_LINES', 5); 

$file = $_SERVER['DOCUMENT_ROOT'] . '/sitemap.txt';                        

// read existing file into array 
$existing = file($file); 

// add this array with the new content 
$addThis = [ "http://www.onlinedealsindia.in/deal/SOME-UNIQUE-LINK\n" ]; 

/* splice array after SKIP_LINES. $existing now has Line 1-5 and $addThis 
    $new contains all lines after the 5th (SKIP_LINES) line */ 
$new = array_splice($existing, SKIP_LINES, 0, $addThis); 

// merge 5 lines with the new content and the other lines 
$new = array_merge($existing, $new); 

// write the new file 
file_put_contents($file, $new);