Announcement

Collapse
No announcement yet.

fwrite don't working as expected

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • fwrite don't working as expected

    We have one blank app, and based on a $_GET var, do a search on DB and it writes some text files.

    PHP Code:
    $codCliente=str_pad $datos[$i][0] , 10 "0" STR_PAD_LEFT );
    $nombre=str_pad $datos[$i][1] , 30 " " STR_PAD_RIGHT );
    $alias=str_pad $datos[$i][2] , 30 " " STR_PAD_RIGHT );
    $orden=str_pad $datos[$i][3] , "0" STR_PAD_LEFT );
    $ultimaVenta=str_pad $datos[$i][4]*100 12 "0" STR_PAD_LEFT );
    $linea=$codCliente.$nombre.$alias.$orden.$ultimaVenta."\r\n";
    fwrite($fichero$linea); 
    This writes this in the text file, and it's ok:
    Code:
    0000000003Pepe                          pepe                          003000000000000
    0000000001Rafael a                      Rafael a                      002000000000000
    Problems comes if strings contains special chars, like , , and so son..
    If I change "Rafael a", for "Rafael " this is what is generate in the file:

    This writes this in the text file, and it's ok:
    Code:
    0000000003Pepe                          pepe                          003000000000000
    0000000001Rafael                      Rafael                      002000000000000
    I guess is something related with with the way caracters is counted, because "" or "" counts 2 bytes.

    The problem is, if I write "Rafael " directly instead retrieveing from the array, txt files are generated correctly.

    Some idea about this?
    /Giuseppe

    Professional Scriptcase Services
    Some Customers opinions

  • #2
    Which database is it? If it is an UTF-8 type database then it would make sense. Dit you perform a SET CHARACTER SET in mysql??
    My guess is that this will help you: http://php.net/manual/en/mysqlinfo.concepts.charset.php

    mb_strlen is the function you may need...
    Last edited by rr; 02-13-2015, 06:23 AM.

    Comment


    • #3
      Originally posted by rr View Post
      Which database is it? If it is an UTF-8 type database then it would make sense. Dit you perform a SET CHARACTER SET in mysql??
      My guess is that this will help you: http://php.net/manual/en/mysqlinfo.concepts.charset.php

      mb_strlen is the function you may need...
      was solved with utf8_encode
      /Giuseppe

      Professional Scriptcase Services
      Some Customers opinions

      Comment

      Working...
      X