Search code examples
phpcsvfputcsv

how to handle comma string in fputcsv in php?


how to put array values in csv in correct format? here is my code:

$title = Business - Research, MPhil/PhD | University of Greenwich | Research degrees;    
$description = '';
$keywords = '';
$list = $title.",".$keywords.",".$description;
$fp = fopen('public/scrap-meta-title-info.csv', 'a+');
        foreach ($list as $line) {
            fputcsv($fp, split(',', $line));
        }
        fclose($fp);

here is my array result: Business - Research, MPhil/PhD | University of Greenwich | Research degrees,, but when i put these values in csv then my title string break and put half value in keyword how i put title value complete in title field?


Solution

  • Your delimiter is , and inside of your title you have a comma. Choose another delimiter, such as ** to concatenate and split on:

    Such as:

    $list = $title."**".$keywords."**".$description;
    
    fputcsv($fp, split('\*\*', $line));