Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Zmiana kodowania, Problem ze zmianą kodowania pliku z UTF8 na ANSII
jibay
post 18.11.2016, 10:28:16
Post #1





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 8.05.2013

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


Witam serdecznie,
mam problem z generowaniem pliku CSV z odpowiednim kodowaniem. Wczytuje plik XML(UTF-8) i na jego podstawie generuje plik CSV, który jest mi potrzebny. Problem w tym, że CSV musi być w kodowaniu ANSI(Windows-1252).
Próbowałem osiągnąć ten efekt funkcją iconv jednak niestety pojawia się informacja "Notice: iconv(): Detected an illegal character in input string".

  1. $local_file = $_FILES["fileToUpload"]["tmp_name"];
  2. $xml=simplexml_load_file($local_file);
  3. $list = array (
  4. array('Nr', 'Tytuł', 'Kontrahent', 'Cena', 'Ilość', 'Wartość', 'Data transakcji', '[k_email]', '[k_adres]', '[k_imie]', '[k_kod]', '[k_miasto]', '[k_nazwisko]', '[k_nick]', '[k_nr]', '[k_firma]', '[k_tel]', '[aukcja_cena_sztuki]', '[aukcja_data]', '[aukcja_nazwa]', '[aukcja_sztuk]')
  5. );
  6. function encodeCSV(&$value, $key){
  7. $value = iconv('UTF-8', 'Windows-1251', $value);
  8. }
  9. foreach ($xml->transaction as $element) {
  10. $orderID=$element->OrderId;
  11. $itemName=$element->Name;
  12. $customerLogin=$element->CustomerLogin;
  13. $itemPrice=$element->positions->position->Price;
  14. $itemPrice = str_replace(".",",",$itemPrice);
  15. $itemQuantity=$element->positions->position->Quantity;
  16. $orderTotal=$element->Total;
  17. $orderTotal = str_replace(".",",",$orderTotal);
  18. $orderDate=$element->SellDate;
  19. $customerEmail=$element->CustomerEmail;
  20. $customerAddress=$element->CustomerAddress;
  21. $customerName=$element->CustomerName;
  22. $customerNameArray = explode(" ", $customerName);
  23. $customerName = $customerNameArray[0];
  24. $customerZip=$element->CustomerZip;
  25. $customerCity=$element->CustomerCity;
  26. $customerSurname=$customerNameArray[1];
  27. $customerId="";
  28. $customerCompany=$element->InvoiceCompanyName;
  29. $customerPhone=$element->CustomerPhone;
  30. $customerPhone = str_replace(' ', '', $customerPhone);
  31. $customerPhone = str_replace('-', '', $customerPhone);
  32. $customerPhone = str_replace('(', '', $customerPhone);
  33. $customerPhone = str_replace(')', '', $customerPhone);
  34. $itemadd = array($orderID, $itemName, $customerLogin, $itemPrice.' zł', $itemQuantity, $orderTotal.' zł', $orderDate, $customerEmail, $customerAddress, $customerName, $customerZip, $customerCity, $customerSurname, $customerLogin, $customerId, $customerCompany, $customerPhone, $itemPrice.' zł', $orderDate, $itemName, $itemQuantity);
  35. array_walk($itemadd, 'encodeCSV');
  36. array_push($list, $itemadd);
  37. }
  38. $fp = fopen('plik.csv', 'w');
  39. foreach ($list as $fields) {
  40. fputcsv($fp, $fields, $delimiter = ';');
  41. }
  42. fclose($fp);
  43. $file="plik.csv";
  44. if (file_exists($file)) {
  45. header('Content-Description: File Transfer');
  46. header('Content-Type: application/octet-stream');
  47. header('Content-Disposition: attachment; filename="'.basename($file).'"');
  48. header('Expires: 0');
  49. header('Cache-Control: must-revalidate');
  50. header('Pragma: public');
  51. header('Content-Length: ' . filesize($file));
  52. readfile($file);
  53. }
Go to the top of the page
+Quote Post
viking
post 18.11.2016, 10:31:02
Post #2





Grupa: Zarejestrowani
Postów: 6 376
Pomógł: 1116
Dołączył: 30.08.2006

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


Zobacz w dokumentacji example //TRANSLIT


--------------------
Go to the top of the page
+Quote Post
Tomplus
post 18.11.2016, 10:54:33
Post #3





Grupa: Zarejestrowani
Postów: 1 872
Pomógł: 230
Dołączył: 20.03.2005
Skąd: Będzin

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


Czasem trzeba się namęczyć z iconv()

Ja miałem problem podobny, aby dane z tablicy UTF-8 przenieść do XML kodowany w W-1250. W pliku nadal są błędy, ale po imporcie XML do nowej bazy, wczytują się z polskimi znakami diakrytycznymi.

  1. setlocale(LC_CTYPE, 'pl_PL');
  2. $string = iconv("UTF-8", "Windows-1250//TRANSLIT", $string);


Go to the top of the page
+Quote Post
jibay
post 18.11.2016, 11:32:25
Post #4





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 8.05.2013

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


Super, kod użytkownika Tomplus działa bez problemu smile.gif Dziękuje.
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 24.04.2025 - 03:54