1

I have a line of data stored in $StripContent, when I echo $Stripcontent it shows

, , ,1 ,1415 ,Fietsen ,Omafietsen ,Avalon ,F 101 Oma Export 57cm Zwart , ,57 ,single speed remnaaf ,2017 ,21 ,249.00 ,135.00 ,19.50 ,8

However when I write $Stripcontent to the CSV file it only writes the first character instead of the whole line.

$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); 
Scott
  • 1,863
  • 2
  • 24
  • 43
Coenfusing
  • 15
  • 4
  • Did you tried anything more? It's working now? – JP. Aulet Jun 07 '17 at 14:18
  • It is working now, I do however, have another problem if you would be so kind to look at: https://stackoverflow.com/questions/44411466/select-specific-lines-and-replace-them-using-php? – Coenfusing Jun 07 '17 at 14:49
  • If its working and solved, mark as solved (this way the SO platforms keeps clean & updated). If the answer helps you, upvote ;) Thanks. – JP. Aulet Jun 07 '17 at 15:13

1 Answers1

0

You can do it more direct with the $Stripcontent variable direclty, whithout the foreach part, like:

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

This works if you want to store the stripContent string. I recomend you to wrap it between a try{...}catch() and check for folder permisions to avoid problems!

Or if you want to use the built-in function for CSV you can use 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);
?>

Hope it helps!

JP. Aulet
  • 4,375
  • 4
  • 26
  • 39