Search code examples
phpcsvfputs

Strip default quotes on some values in fputs


I need to export some values to an csv using php and I need to strip the first two values in my array of its default quotes (even if it has a space) and keep the default quotes on the last value.

So the result I am looking for in the exported csv is:

my name,01-12 00,"Is 50";

123232,2111 Po,"120222 dasd dd";

and so on...

If I use the following script it removes all the quotes, but I need to keep the quotes on the last value. Does anyone have any ideas on how to solve this?

$array = array($xxx,$yyy,$vvv);
$array = str_replace('"', '', $array);
fputs($fo, implode($array, ',')."\n");

Many thanks

Best regards


Solution

  • If I understand you right, the simplest solution will be like this:

    $array = array('my name','01-12 00','"Is 50"');
    $last  = array_pop($array);
    $array = str_replace('"', '', $array);
    array_push($array, $last);
    fputs($fo, implode($array, ',')."\n");