2016-04-28 40 views
3

所需的解決方案:爲什麼它會在下一行的開始處添加文本,而不是在正確的末尾?

我用一個簡單的PHP腳本的工作應該:

  1. 添加「VALUE4 :::」在第一行的一個文件末尾
  2. 添加「 :::「在所有下一行的末尾

我在編程方面很努力,並且在這裏坐了兩天試圖找出答案。可能是一個小細節,或者它可能是完成這個任務的完全錯誤的方式。

這可能是錯誤的方法,或者可能是一個正則表達式問題?我真的不知道。

我懇請您幫助解決方案。


信息:

文件newstest.db看起來是這樣的:

ID:::value1:::value2:::value3::: 
1:::My:::first:::line::: 
2:::My:::second:::line::: 
3:::Your:::third:::line::: 

並使用該PHP腳本,我想使它看起來像這樣:

ID:::value1:::value2:::value3:::value4::: 
1:::My:::first:::line:::::: 
2:::My:::second:::line:::::: 
3:::Your:::third:::line:::::: 

問題:

到目前爲止,我還差點,但我很困惑,爲什麼它添加「VALUE4 :::」到開始,然後添加「 :::「在所有其餘行的開頭(不是結尾)。

所以我得到類似如下的文件:

ID:::value1:::value2:::value3::: 
value4:::1:::My:::first:::line::: 
:::2:::My:::second:::line::: 
:::3:::Your:::third:::line::: 

我認爲:

$lineshere = 'Some text here:::';  
$lines1 = $linehere.'value4:::'; 

將輸出 「有些文字在這裏::: VALUE4 :::」

可能問題是由於這種方式後續添加行嗎?

$lines = ''; 
$lines.= 'My test'; 
$lines.= ' is here'; 
echo $lines; 

我是編程的新手,所以我可能使用了完全錯誤的函數tec來完成這項工作。

但是在這種情況下,它會在錯誤的地方添加空格或換行符/行結尾。


我對這個問題的解決方案嘗試:

<?php 
// specify the file 
$file_source="newstest.db"; 

// get the content of the file 
$newscontent = file($file_source, true); 

//set a start value (clear memory) 
$lines =''; 

// get each line and treat it line by line. 

foreach ($newscontent as $line_num => $linehere) { 

    // add "value4:::" at the end of FIRST line only, and put it in memory $lines 
    if($line_num==0) { 
     $lines.= $linehere.'value4:::'; 

     // Just to see what the line looks like 
     //echo 'First line: '.$lines.'<br /><br />'; 
    } 

    // then add ":::" to the other lines and add them to memory $lines 
    if($line_num>0) { 
     $lines1 = $linehere.':::'; 
     $lines.= $lines1; 

     //just look at the line 
     //echo 'Line #'.$line_num.': '.$lines1.'<br /><br />'; 
    } 
} 

//Write new content to $file_source 
$f = fopen($file_source, 'w'); 
fwrite($f,$lines); 
fclose($f); 

echo "// to show the results ar array<br /><br />"; 

$newscontentlook = file($file_source, true); 
print_r(array_values($newscontentlook)); 
?> 
+1

正如一個側面說明:您可能想看看使用十六進制編輯器,看看換行/回車字符和文件。 –

+0

我相信,這是因爲$ linehere以換行符結束。 –

+0

'$ newscontent = file($ file_source,true)''true'的目的是什麼;'?將其更改爲「FILE_IGNORE_NEW_LINES」,它應該可以工作。 – ClasG

回答

2

這其實是很容易實現與file_get_contentspreg_replace,即:

$content = file_get_contents("newstest.db"); 
$content = preg_replace('/(^ID:.*\S)/im', '$1value4:::', $content); 
$content = preg_replace('/(^\d+.*\S)/im', '$1:::', $content); 
file_put_contents("newstest.db", $content); 

輸出:

ID:::value1:::value2:::value3:::value4::: 
1:::My:::first:::line:::::: 
2:::My:::second:::line:::::: 
3:::Your:::third:::line:::::: 

Ideone Demo

+0

這是一個聰明的做法。但我有一個問題。它現在在第一行下面添加「value4 :::」作爲新行,等等。每個新的「:::」都相同。任何想法爲什麼?它與我得到文件內容的方式有關嗎? – Preben

+1

嘗試在兩個正則表達式的末尾添加'\ S',我更新了答案。 '\ S'匹配不是「空白字符」的單個字符(任何Unicode分隔符,製表符,換行符,回車符,垂直製表符,換行符,下一行)«\ S» –

+0

對不起。當談到正則表達式時,我真的很低調。我不明白在哪裏放'\ S'。我試過'im'之後,但沒有奏效。它應該怎麼看? – Preben

2

功能file()讓你在每一行作爲數組元素的數組文件。該行的每個結尾(EOL)都包含在元素中。因此,向該行添加文本將位於EOL之後,因此在下一行的開頭有效。

您可以選擇不使用file()並在EOL上自行分解文本,因此EOL不再是數組元素的一部分。然後編輯這些元素,並再次將數組內爆。

fopen()該文件,和fread()自己通過它。後一個選項是首選,因爲它不會一次將整個文件加載到內存中。

+0

很好的解釋。可以用當前的方法解決,通過添加標誌'FILE_IGNORE_NEW_LINES'(如果我不完全錯誤)。 – ClasG

1

我認爲這個問題是通過文件中讀取行的循環()函數:

foreach ($newscontent as $line_num => $linehere) { 
    ... 

$linehere在結尾處包含一個換行符,所以你應該簡單地使用它之前砍它:

foreach ($newscontent as $line_num => $linehere) { 
    $linehere = chop($linehere); 
    ... 

如果不這樣做chop行內容,當您將字符串到它,你會得到:

LINE_CONTENTS\nSTRING_ADDED 

其中,在打印時,將是:

LINE_CONTENTS 
STRING_ADDED 

希望這有助於...

+0

這解決了空間問題:-)但是,現在當寫回文件時,所有內容都保存在一行中。所以我可能需要添加一個代碼,告訴它將每行保存爲「行尾」?你知不知道怎麼? – Preben

+1

是的:你可以在寫每行時使用'fputs()'而不是'fwrite()',或者在'fwrite()'之前的每行末尾添加'\ n'字符...... – MarcoS

相關問題