2017-06-02 36 views
0

我有一個行存儲在$StripContent數據的,當我回聲$Stripcontent它顯示
書寫內容存儲在變量csv文件

,,,1,1415,Fietsen,Omafietsen,阿瓦隆,F 101大間出口57釐米茲瓦特,57,單速remnaaf,2017年,21,249.00,135.00,19.50,8

然而,當我寫$Stripcontent的CSV文件只寫第一個字符,而不是整條生產線。

$Get_Content = file_get_contents($newname); 

$StripContent = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",',', $Get_Content); 

$file = $newname; 
$content = file($file); 
foreach($content as $lineNumber => &$lineContent) { 
    if($lineNumber == 0) { 
     $lineContent .= $StripContent . PHP_EOL; 
    } 
} 

$allContent = implode("", $content); 
file_put_contents($file, $allContent); 
+0

你有沒有嘗試過更多?它現在在工作嗎? –

+0

它現在正在工作,但我確實有另一個問題,如果你會這麼友好的看看:https://stackoverflow.com/questions/44411466/select-specific-lines-and-replace-them-using-php ? – Coenfusing

+0

如果它的工作和解決,標誌着解決(這樣的SO平臺保持清潔和更新)。如果答案幫助你,upvote;)謝謝。 –

回答

0

你可以做到這一點與$Stripcontent變量照片直接,更直接的內部消除部分foreach,如:

$Get_Content = file_get_contents($newname); 

$StripContent = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",',', $Get_Content); 

$file = $newname; 
$content = file($file); 
file_put_contents($file, $Stripcontent); 

,如果你要存儲stripContent字符串這工作。我建議你將它包裝在try{...}catch()之間並檢查文件夾權限以避免問題!

或者,如果你想使用CSV內置的功能,您可以使用fputcsvoffical documentation):

<?php 

$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'), 
    array('123', '456', '789'), 
    array('"aaa"', '"bbb"') 
); 

$fp = fopen('file.csv', 'w'); 

foreach ($list as $fields) { 
    fputcsv($fp, $fields); 
} 

fclose($fp); 
?> 

希望它能幫助!