2
I have a txt file extracted from a PDF. I upload this txt via php, and need to remove the line break between the description items and put in an array, follows:
TXT ORIGINAL:
0235.000001-4 0235.213.00001295-8
`DUAS ALIANÇAS, NOVE ANÉIS, QUATRO BRINCOS, UM COLAR, SETE
PENDENTES, QUATRO PULSEIRAS, DE: OURO BRANCO, OURO, PRATA
PALÁDIO, OURO BAIXO; CONTÉM: pedra, diamantes; CONSTAM:
amassada(s), defeito(s), falta, iniciais, incompleta(s), inscrições,
partida(s), PESO LOTE: 37,80G (TRINTA E SETE GRAMAS E OITENTA CENTIGRAMAS)
R$ 2.198,00
Valor Grama: 58,15
EXPECTED ARRAY RESULT
array
0 =>
array
0 => 0235.000001-4 0235.213.00001295-8
1 => 'DUAS ALIANÇAS, NOVE ANÉIS, QUATRO BRINCOS, UM COLAR, SETE PENDENTES, QUATRO PULSEIRAS, DE: OURO BRANCO, OURO, PRATA PALÁDIO, OURO BAIXO; CONTÉM: pedra, diamantes; CONSTAM: amassada(s), defeito(s), falta, iniciais, incompleta(s), inscrições, partida(s), PESO LOTE: 37,80G (TRINTA E SETE GRAMAS E OITENTA CENTIGRAMAS)'
2 => R$ 2.198,00
3 => 'Valor Grama: 58,15'
1 =>
array
0 =>
1 =>
2 =>
3 =>
Follow the code I’m using:
$file = fopen($fileName,"r") or exit("Unable to open file!");
if ( $file !== false ) {
$i = 0;
$members = Array();
while (!feof($file)) {
$string = fgets($file);
$string = preg_replace( "/\r|\n/", "", $string );
$members[$i][] = $string;
}
var_dump($members);
fclose($file);
}
However I am not able to put the description of the lot in a single line of the array, follows the result of the above code:
array (size=1)
0 =>
array (size=30952)
0 => string '' (length=3)
1 => string '0235.000001-4 0235.213.00001295-8 ' (length=34)
2 => string '' (length=0)
3 => string 'DUAS ALIANÇAS, NOVE ANÉIS, QUATRO BRINCOS, UM COLAR, SETE ' (length=61)
4 => string 'PENDENTES, QUATRO PULSEIRAS, DE: OURO BRANCO, OURO, PRATA ' (length=59)
5 => string 'PALÁDIO, OURO BAIXO; CONTÉM: pedra, diamantes; CONSTAM: ' (length=59)
6 => string 'amassada(s), defeito(s), falta, iniciais, incompleta(s), inscrições, partida(s), ' (length=84)
7 => string 'PESO LOTE: 37,80G (TRINTA E SETE GRAMAS E OITENTA CENTIGRAMAS) ' (length=63)
8 => string '' (length=0)
9 => string 'R$ 2.198,00 ' (length=12)
10 => string 'Valor Grama: 58,15 ' (length=19)
11 => string '' (length=0)
Could someone help?
Your question has been resolved
– Jorge Costa
You need to treat your business rule within the loop. In what you described, you can consider the blank line as the separator of the values you are looking for. To remove spaces at the ends of a string we use the Trim function()
– gpupo
Trim isn’t working either, because it removes the spaces but doesn’t put everything in a single position in the array.
– RDARE