Open csv file improper encoding - php

When I open the csv file
$str = file_get_contents($filename);
$str = mb_convert_encoding($str,"utf-8","sjis");
var_dump($str);
It shows like this
\0000\000.\0000\0000\0008\0000\0005\0007\000,\0000\000.\0000\0001\0000 ....
At first I think , this is the problem of encoding, so I added mb_convert_encoding but in vain.
What cause this improper encoding?

Better you can do like this:
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
Code copied from :http://php.net/manual/en/function.fgetcsv.php
alternatively : http://php.net/manual/en/function.str-getcsv.php

Related

How to adapt PHP CSV utf-8?

I read a csv file with php and print it to the screen with echo.
After printing, there is a utf-8 character problem in the output.
how can i solve this problem?
enter image description here
<?php
$row = 1;
if (($handle = fopen("Liste.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $row satırındaki $num alan: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
// convert encoding
$data = mb_convert_encoding($data, "UTF-8", "auto");
// str_getcsv
$array = str_getcsv($data);
?>

PHP Encoding type

I have problems with encoding csv file and showing it.
I found function like:
$row = 1;
if (($handle = fopen("1611005286991218.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
//echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
And it's opening but not encoded. This is the first lines of output:
PK!��Y^�[Content_Types].xml
�(����N�0E�H���-Jܲ#5��*Q>�ēƪ_��{&.-�V���J
[�dM���J���r5���Lҽ`>�蛃�����1GLx��gѣ֭��^S�|����A:<��R�"���t�E
!�I�wV�N�ޒRx��˹�g.�e� ��PK!�E��O_rels/.rels
�(���MO�0��H�����ݐBKwAH�!T~�I����$ݿ'\�Jcp��׏_y���Q9�^��uQ�bg�����R?��#�D��(�5�8®���>�H)Ů�Qe5t)�{�h:�(���J#a���Т'3P˸)�[�5�Zh�����T}�y����4��1��]:�yN�
ە�-�>_�j
-' V�SNG$ �#m����8q"K��H��#���i���/��$�"�'.ޠz��PK! Go��xl/_rels/workbook.xml.rels �(��R�j�0��b�촔R"�R
���im�ؒ�n��
Bz�eavٙ��v�3���+��zl�;���b���G��ۛ��s�>�� �8��
I tried encoding it with UTF-8, but no success, like so:
echo mb_convert_encoding($data[$c], 'UTF-8', 'windows-1251');
Anyone had similar problems?

Retrieve columns data for each line csv

I want to retrieve for each line all data for a specific column of a csv file.
File structure
Swimlane,Column,ID,Title
For example retrieve all data for the column "Title".
Code:
$row = 1;
if (($handle = fopen("Business Backlog API Publication.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// Column title
$num = $data[4];
echo "<p> $num : $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
Thank you so much
If file is not huge, you can use file() function:
$lines = file("Business Backlog API Publication.csv");
$row = 0;
foreach($lines as $line)
{
$line = explode(",", $line);
$title = $line[3];
echo "<p> $row : $title: <br /></p>\n";
$row++;
}
Note that title is at index 3, not 4

PHP open csv and save data to new csv

I need to replace a lot of data in csv and after changing all things i want it saved to a new csv file.
i can read the csv with the code from php.net but i can't get it to work saving the new file. i have this
$row = 0;
if (($handle = fopen("original.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 20000, ";")) !== FALSE) {
$num = count($data);
$cat_alt = ['cold', 'hot', ... and so on...];
$cat_neu = ['kalt', 'heiß', ...und so weiter...];
echo "<p> $num Felder in Zeile $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
$output = str_replace($cat_alt, $cat_neu, $data[$c] . "<br />\n");
echo $output;
}
}
}
$fp = fopen('changed.csv', 'w');
foreach ($row as $rows) {
fputcsv($fp, $rows);
}fclose($fp);
You have two options:
Use n array
$rows = []
...
$rows[] = str_replace($cat_alt, $cat_neu, $data[$c] . "\n");
...
fputcsv($fp, $rows);
Open the output file beforehand and write to it as you process the data
$fp = fopen('changed.csv', 'w');
if (($handle = fopen("original.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 20000, ";")) !== FALSE) {
$num = count($data);
$cat_alt = ['cold', 'hot', ... and so on...];
$cat_neu = ['kalt', 'heiß', ...und so weiter...];
echo "<p> $num Felder in Zeile $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
fputs($fp, str_replace($cat_alt, $cat_neu, $data[$c] . "<br />\n"));
}
}
}
fclose($fp);

How to parse excel contents from excel document using php

I have an excel document with many columns.I want to parse that data.Please help in php.Provide simple means to guide
Thanks
look at this class
http://phpexcel.codeplex.com/
you can find an example of read here
if it saves as CSV this is how to read it:
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
I've used this in one of my projects...works like a charm :P
http://sourceforge.net/projects/phpexcelreader/

Categories