0

I'm writting file and in a field, I need write 6 caracters from number and complete with space. I use then str_pad

str_pad($globalResult['Folio'], 6, ' ', STR_PAD_LEFT)

But In my file I see it's adding quotes around my field. I put for example 6 and I've in return " 6". I want same but without quotes.

I try

trim(str_pad($globalResult['Folio'], 6, ' ', STR_PAD_LEFT),'"')

Or

str_pad($globalResult['Folio'], 6, ' ', STR_PAD_LEFT))

But I've always " 6" in my file.

I try $globalResult['Folio'] only I've 6.

Thanks for your help

Update

Here a little more code:

$df = fopen("facturation/PDF/Export/ECRITURE.WIN", 'w');

//BOUCLE D'AFFICHAGE DES FACTURES====
//On parcourt la liste des factures
$DB_GLOBAL->DbQuery($req)or die(mysql_error());
$NumRow=1;
while($globalResult = $DB_GLOBAL->DbNextRow())
{
    $inf = [$globalResult['Typ'],$globalResult['Dte'],str_pad($globalResult['Folio'], 6, ' ', STR_PAD_LEFT), str_pad($NumRow, 6, ' ', STR_PAD_LEFT)];
    fputcsv($df, $inf, '|');
    $NumRow++;
}
if (fclose($df) === true) {
echo 'Fichier cree';

} else {
      echo 'Erreur : contactez l\'administrateur.';
}

And the result in file

VE|05012015|" 1"|" 1" VE|05012015|" 1"|" 2" VE|05012015|" 1"|" 3" VE|06012015|" 1"|" 4" VE|06012015|" 1"|" 5" VE|06012015|" 1"|" 6" VE|06012015|" 1"|" 7" VE|07012015|" 1"|" 8" VE|07012015|" 1"|" 9" VE|07012015|" 1"|" 10" VE|08012015|" 1"|" 11" VE|08012015|" 1"|" 12" VE|08012015|" 1"|" 13" VE|08012015|" 1"|" 14" VE|08012015|" 1"|" 15" VE|09012015|" 1"|" 16" VE|09012015|" 1"|" 17" VE|09012015|" 1"|" 18" VE|09012015|" 1"|" 19" VE|10012015|" 1"|" 20" VE|10012015|" 1"|" 21"

I try to force enclosure parameter of fputcsv, "\n", "\0", null but nothing like I want. I need to export some datas on another application. I need to respect format asked

YannickIngenierie
  • 602
  • 1
  • 13
  • 37
  • `str_pad` does not write to file, maybe problem in write function? – Iłya Bursov Jan 28 '16 at 17:00
  • str_pad does not add quotes, see https://eval.in/509448 – syck Jan 28 '16 at 17:01
  • Don't take me for a dumb, please. I know str_pad is not for writting in a file... I use fputcsv. But I written severals integer or date field without problem. I just describe where my problem is... And I show severals options I find on stackOverflow to delete quote, but no success. It's why I ask for help. – YannickIngenierie Jan 29 '16 at 12:14

2 Answers2

1

Use intval to convert a string to an integer number:

$folio_int = intval(str_pad($globalResult['Folio'], 6, ' ', STR_PAD_LEFT));

fputcsv encloses all strings that contain either whitespace (a space) or the encapsulation character (usually ").
You can either save it as integer (and lose your space), or use fputs and construct the csv file yourself. The problem has nothing to do with str_pad and only is because of the fputcsv function.

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
  • I try your solution, but it's deleting space I just add with str_pad. I've a number to complete on 6 caracters with space on the left. – YannickIngenierie Jan 29 '16 at 12:19
  • 1
    It ads the " because you save it in csv format and fputcsv always encapsulate strings, since you want to preserve the space it will always add the " around it. You can better use `fputs` and construct the csv file yourself. – Niki van Stein Jan 29 '16 at 13:54
0

I find here

fputs($df, implode($inf, ',')."\n");

Work perfectly

Community
  • 1
  • 1
YannickIngenierie
  • 602
  • 1
  • 13
  • 37