Print non-ASCII characters in a CSV file

I am trying to create a CSV file using php. How to print characters without ascii?

+3
source share
7 answers

Unicode characters can be used in CSV files, just make sure you use the correct HTTP headers. This works fine in OpenOffice, but if I remember that the correct Excel has problems displaying CSV files with Unicode characters.

In addition, you should try to use fputcsv , this will simplify the situation. When you create files on the fly, you can use php output stream .

So something like this:

$handle = fopen("php://output", "w");

header("Content-Type: text/csv; charset=UTF-8");
fputcsv($handle, $fields, ';', '"');

fclose($handle);


, htmlentities, é. , , . html_entity_decode :

$decoded_string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');

Btw, htmlentities , , html ( ), . unicode.

+10

fputcsv utf-8.

+4

, , , .

sql, csv .

<?php
function exportMysqlToCsv($csvsql,$filename = 'export.csv')
{
    $csv_terminated = "\n";
    $csv_separator = ",";
    $csv_enclosed = '"';
    $csv_escaped = "\\";
    $sql_query = $csvsql;

    // Gets the data from the database
    $result = mysql_query($sql_query);
    $fields_cnt = mysql_num_fields($result);


    $schema_insert = '';

    for ($i = 0; $i < $fields_cnt; $i++)
    {
        $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
            stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
        $schema_insert .= $l;
        $schema_insert .= $csv_separator;
    } // end for

    $out = trim(substr($schema_insert, 0, -1));
    $out .= $csv_terminated;

    // Format the data
    while ($row = mysql_fetch_array($result))
    {
        $schema_insert = '';
        for ($j = 0; $j < $fields_cnt; $j++)
        {
            if ($row[$j] == '0' || $row[$j] != '')
            {

                if ($csv_enclosed == '')
                {
                    $schema_insert .= $row[$j];
                } else
                {
                    $meta = mysql_fetch_field($result, $j);
                    if($meta->type == "int" || $meta->type == "real")
                    {
                      $schema_insert .= $row[$j];
                    } else {
                      $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
                    }
                }
            } else
            {
                $schema_insert .= '';
            }

            if ($j < $fields_cnt - 1)
            {
                $schema_insert .= $csv_separator;
            }
        } // end for

        $out .= $schema_insert;
        $out .= $csv_terminated;
    } // end while

    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: " . strlen($out));
    // Output to browser with appropriate mime type, you choose ;)
    header("Content-type: text/x-csv");
    //header("Content-type: text/csv");
    //header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=$filename");
    echo $out;
    exit;

} 
?>
+1

, " " (.. echo .., ), :

1) PHP-:

 header ('Content-type: text/csv; charset=utf-8');

2) HTML:

 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

3) PHP UTF-8 .

0

-, , . UTF-8. , , , UTF.
, , . Firefox menu- > view- > .

0

shamittomar ,

Your problem is with your encoding,
you have to convert text encoding to UTF-8 since php uses ascii inside

example:

$str = mb_convert_encoding($str , "UTF-8") ; 

contact php.net for more information

0
source

The best example I have found is one.       

    function str_to_csv($row) {
        if ($row == '') {
            return array();
        }
        $a = array();
        $src = explode(',', $row);
        do {
            $p = array_shift($src);
            while (mb_substr_count($p, '"') % 2 != 0) {
                if (count($src) == 0) {
                    return false;
                }
                $p .= ',' . array_shift($src);
            }
            $match = null;
            if (preg_match('/^"(.+)"[\r\n]*$/', $p, $match)) {
                $p = $match[1];
            }
            $a[] = str_replace('""', '"', $p);
        } while (count($src) > 0);
        return $a;
    }

    function file_getcsv($f) {
        $line = fgets($f);
        while (($a = str_to_csv($line)) === false) {
            if (feof($f)) {
                return false;
            }
            $line .= "\n" . fgets($f);
        }
        return $a;
    }

    function file_to_csv($filename) {
        ini_set("auto_detect_line_endings", true);
        $a = array();
        $f = fopen($filename, 'r');
        while (!feof($f)) {
            $rec = file_getcsv($f);
            if ($rec === false) {
                return false;
            }
            if (!empty($rec)) {
                $a[] = $rec;
            }
        }
        fclose($f);
        return $a;
    }

    $data = file_to_csv('base2.csv');

    echo '<pre>';
    print_r($data);
0
source

Source: https://habr.com/ru/post/1761777/


All Articles