Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Problem z tablica z polskimi znakiami
tomas3man
post
Post #1





Grupa: Zarejestrowani
Postów: 10
Pomógł: 0
Dołączył: 22.09.2014

Ostrzeżenie: (0%)
-----


Witam,

jestem poczatkujacy w temacie PHP i oraz pisze pierwszy raz na tym forum. Dlatego z gory przepraszam za bledy.

Mam problem z wczytaniem pliku tekstowego, z danymi przelewow bankowch w formacie zblizonym do CSV (nie prawdziwy CSV). Najpier wczytuje plik do zmiennej $content.

Potem parsuje w petli zmienna $content w poszukiwaniu oddzielonych przecinkami, lub innymi znakami separacji pol danych, pole po polu i linia po linni.
Parsowane dane sa wrzucane do wielowymiarowej tablicy, gdzie lokalizuje wszystkie przelwy oraz wszystkie pola dla danego przelewu.
Skrypt pracuje poprawnie jak w polach tekstowych nie ma polskiego znaku. Jezeli chociaz w jednym polu danego wiersza znajduje sie polski znak, to wszystkie elementy tablicy dotyczacej danego wiersza sa puste.
Dlaczego?

Tutaj wrzuam petle ktora parsuje:

  1.  
  2. $buffer = '';
  3. $field = 't_date';
  4. $row=0;
  5. $cvs = array();
  6. $byte_number = 0;
  7. $lenght = strlen($content);
  8.  
  9. echo "<font color=white><pre>";
  10. echo $content;
  11.  
  12. while( $byte_number <= $lenght) {
  13. $ch = $content[$byte_number]; $byte_number++;
  14. echo '<br>'.'('.$ch.') ('.ord($ch).') '; echo $field;
  15.  
  16. if ( $ch == ' ' ) {
  17. $save = $ch;
  18. while ( $ch == ' ' ) {
  19. $ch = $content[$byte_number]; $byte_number++;
  20. }
  21. $ch = $save.$ch;
  22. }
  23. if ( $ch == ';' and $field == 't_date' ) {
  24. $cvs[$field]=$buffer; echo debug($cvs);
  25. $buffer = '';
  26. $field ='t_acounting_date';
  27. $ch = '';
  28. }
  29. if ( $ch == ';' and $field == 't_acounting_date' ) {
  30. $cvs[$field]=$buffer; echo debug($cvs);
  31. $buffer = '';
  32. $field ='t_side';
  33. $ch = '';
  34. }
  35. if ( $ch == ';' and $field == 't_side' ) {
  36. $cvs[$field]=$buffer; echo debug($cvs);
  37. $buffer = '';
  38. $field ='t_title';
  39. $ch = '';
  40. }
  41. if ( $ch == ';' and $field == 't_title' ) {
  42. $cvs[$field] = substr ($buffer, 1, -1); echo debug($cvs);
  43. $buffer = '';
  44. $field ='t_sender';
  45. $ch = '';
  46. }
  47. if ( $ch == ';' and $field == 't_sender' ) {
  48. $cvs[$field] = substr ($buffer, 1, -1) ; echo debug($cvs);
  49. $buffer = '';
  50. $field ='t_account';
  51. $ch = '';
  52. }
  53. if ( $ch == ';' and $field == 't_account' ) {
  54. $cvs[$field] = substr ($buffer, 1, -1); echo debug($cvs);
  55. $buffer = '';
  56. $field ='t_volume';
  57. $ch = '';
  58. }
  59. if ( $ch == ';' and $field == 't_volume' ) {
  60. $cvs[$field] = substr ($buffer, 0, -3); echo debug($cvs);
  61. $buffer = '';
  62. $field ='t_balance';
  63. $ch = '';
  64. }
  65. if ( $ch == ';' and $field == 't_balance' ) {
  66. $cvs[$field] = $buffer; echo debug($cvs);
  67. $ch = $content[$byte_number]; $byte_number++;
  68. $ch = $content[$byte_number]; $byte_number++;
  69. $row++;
  70. $field = 't_date';
  71. $ch = '';
  72. $buffer = '';
  73. $cvs = '';
  74. }
  75. $buffer .= $ch; echo ' '.$buffer;
  76.  
  77. }



Nie moge wrzucic screen shootu, ale wglada to w ten sposob, ze kiedy pointer parsujacy natrafi na bajt zawierajacy polski znak, to element tablicy jest pelen znakow, dopiero kiedy pointer parsujacy dojdzie do konca pola, to element tablicy zastepowany jest nul.

Tutaj wklejam wynik ekranu podczas uruchomienia kodu, w celu debugu dodalem tam wyswietlanie zmiennych:

  1.  
  2. (2) (50) t_date 2
  3. (0) (48) t_date 20
  4. (1) (49) t_date 201
  5. (4) (52) t_date 2014
  6. (-) (45) t_date 2014-
  7. (0) (48) t_date 2014-0
  8. (9) (57) t_date 2014-09
  9. (-) (45) t_date 2014-09-
  10. (0) (48) t_date 2014-09-0
  11. (9) (57) t_date 2014-09-09
  12. (;) (59) t_date
  13. /app/Controller/PaymentsController.php (line 378)
  14.  
  15. 't_date' => '2014-09-09'
  16. )
  17.  
  18.  
  19. (2) (50) t_acounting_date 2
  20. (0) (48) t_acounting_date 20
  21. (1) (49) t_acounting_date 201
  22. (4) (52) t_acounting_date 2014
  23. (-) (45) t_acounting_date 2014-
  24. (0) (48) t_acounting_date 2014-0
  25. (9) (57) t_acounting_date 2014-09
  26. (-) (45) t_acounting_date 2014-09-
  27. (0) (48) t_acounting_date 2014-09-0
  28. (9) (57) t_acounting_date 2014-09-09
  29. (;) (59) t_acounting_date
  30. /app/Controller/PaymentsController.php (line 384)
  31.  
  32. 't_date' => '2014-09-09',
  33. 't_acounting_date' => '2014-09-09'
  34. )
  35.  
  36.  
  37. (P) (80) t_side P
  38. (R) (82) t_side PR
  39. (Z) (90) t_side PRZ
  40. (E) (69) t_side PRZE
  41. (L) (76) t_side PRZEL
  42. (E) (69) t_side PRZELE
  43. (W) (87) t_side PRZELEW
  44. ( ) (32) t_side PRZELEW Z
  45. (E) (69) t_side PRZELEW ZE
  46. (W) (87) t_side PRZELEW ZEW
  47. (N) (78) t_side PRZELEW ZEWN
  48. (E) (69) t_side PRZELEW ZEWNE
  49. (T) (84) t_side PRZELEW ZEWNET
  50. (R) (82) t_side PRZELEW ZEWNETR
  51. (Z) (90) t_side PRZELEW ZEWNETRZ
  52. (N) (78) t_side PRZELEW ZEWNETRZN
  53. (Y) (89) t_side PRZELEW ZEWNETRZNY
  54. ( ) (32) t_side PRZELEW ZEWNETRZNY P
  55. (R) (82) t_side PRZELEW ZEWNETRZNY PR
  56. (Z) (90) t_side PRZELEW ZEWNETRZNY PRZ
  57. (Y) (89) t_side PRZELEW ZEWNETRZNY PRZY
  58. (C) (67) t_side PRZELEW ZEWNETRZNY PRZYC
  59. (H) (72) t_side PRZELEW ZEWNETRZNY PRZYCH
  60. (O) (79) t_side PRZELEW ZEWNETRZNY PRZYCHO
  61. (D) (68) t_side PRZELEW ZEWNETRZNY PRZYCHOD
  62. (Z) (90) t_side PRZELEW ZEWNETRZNY PRZYCHODZ
  63. (A) (65) t_side PRZELEW ZEWNETRZNY PRZYCHODZA
  64. (C) (67) t_side PRZELEW ZEWNETRZNY PRZYCHODZAC
  65. (Y) (89) t_side PRZELEW ZEWNETRZNY PRZYCHODZACY
  66. (;) (59) t_side
  67. /app/Controller/PaymentsController.php (line 390)
  68.  
  69. 't_date' => '2014-09-09',
  70. 't_acounting_date' => '2014-09-09',
  71. 't_side' => 'PRZELEW ZEWNETRZNY PRZYCHODZACY'
  72. )
  73.  
  74.  
  75. (") (34) t_title "
  76. (O) (79) t_title "O
  77. (P) (80) t_title "OP
  78. (L) (76) t_title "OPL
  79. (A) (65) t_title "OPLA
  80. (T) (84) t_title "OPLAT
  81. (A) (65) t_title "OPLATA
  82. ( ) (32) t_title "OPLATA Z
  83. (A) (65) t_title "OPLATA ZA
  84. ( ) (32) t_title "OPLATA ZA K
  85. (U) (85) t_title "OPLATA ZA KU
  86. (R) (82) t_title "OPLATA ZA KUR
  87. (S) (83) t_title "OPLATA ZA KURS
  88. (,) (44) t_title "OPLATA ZA KURS,
  89. ( ) (32) t_title "OPLATA ZA KURS, M
  90. (I) (73) t_title "OPLATA ZA KURS, MI
  91. (C) (67) t_title "OPLATA ZA KURS, MIC
  92. (H) (72) t_title "OPLATA ZA KURS, MICH
  93. (A) (65) t_title "OPLATA ZA KURS, MICHA
  94. (Ł) (163) t_title "OPLATA ZA KURS, MICHAŁ
  95. ( ) (32) t_title "OPLATA ZA KURS, MICHAŁ G
  96. (R) (82) t_title "OPLATA ZA KURS, MICHAŁ GR
  97. (A) (65) t_title "OPLATA ZA KURS, MICHAŁ GRA
  98. (B) (66) t_title "OPLATA ZA KURS, MICHAŁ GRAB
  99. (C) (67) t_title "OPLATA ZA KURS, MICHAŁ GRABC
  100. (Z) (90) t_title "OPLATA ZA KURS, MICHAŁ GRABCZ
  101. (A) (65) t_title "OPLATA ZA KURS, MICHAŁ GRABCZA
  102. (K) (75) t_title "OPLATA ZA KURS, MICHAŁ GRABCZAK
  103. (") (34) t_title "OPLATA ZA KURS, MICHAŁ GRABCZAK"
  104. (;) (59) t_title
  105. /app/Controller/PaymentsController.php (line 396)
  106.  
  107.  


Dla wygoty debugowania w nawiasie wyswietla znak (A) oraz jego kod ASCII (65) przy kazdym wczytanym znaku.
Widac ze skrypt prawidlowo odczytal pola w liniach drugiego listningu debugujacego 13,31,69 i tablica $csv zawiera dane,
ale poczawszy od linii (tu mowie caly czas o debugu) 108, tablic $csv znika, zamienia sie w null, bo w polu byl polski znak,
widac to w linni 97.
Jezeli usune polski znak Ł z pliku tekstowego, to petla parsujaca przejdzie poprawnie.

Czy ktos moze mi pomoc?
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.12.2025 - 20:04