2017-06-02 10 views
0

私は$Stripcontentエコーときに
書き込み内容は、CSVファイルに変数に格納されている

、、、1、1415 Fietsen、Omafietsen、アバロン、F 101大間を示し、$StripContentに格納されたデータのラインを有しています輸出57センチメートルズワルト、57、シングルスピードremnaaf、2017、21、249.00、135.00、19.50、

8は、しかし、私はそれをCSVファイルに$Stripcontentを書くだけで全体ではなく、行の最初の文字を書き込みます。

$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

あなたは次のように、foreach一部whithout、$Stripcontent変数direcltyとより直接的行うことができます:あなたはstripContent文字列を格納したい場合は、この作品

$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); 

。私はtry{...}catch()の間にそれを包み込み、問題を避けるためにフォルダパーミッションをチェックすることをお勧めします!

それとも、CSVのための組み込み関数を使用したい場合は、fputcsvを使用することができます(offical 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); 
?> 

はそれが役に立てば幸い!

関連する問題