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.

$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] , 3 , "0" , STR_PAD_LEFT );
$ultimaVenta=str_pad ( $datos[$i][4]*100 , 12 , "0" , STR_PAD_LEFT );
$linea=$codCliente.$nombre.$alias.$orden.$ultimaVenta."
";
fwrite($fichero, $linea);

This writes this in the text file, and it’s ok:

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:

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?

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…

[QUOTE=rr;32583]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…[/QUOTE]

was solved with utf8_encode