且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用AJAX将Windows-1252转换为UTF-8

更新时间:2023-11-27 13:51:04

由于数据的字符集是Windows-1252,因此应将test3.php中的字符集值从UTF-8更改为Windows-1252.

因此您需要替换:

<meta charset="UTF-8"><meta charset="Windows-1252">

I have strings with some dutch characters in a csv file which I should read in PHP and JS.

For PHP I am using the iconv( "Windows-1252", "UTF-8", $str ); approach to convert Windows-1252 to UTF-8 format. It works fine when I use fgetcsv and iconv.

DEMO IN PHP

CODE IN PHP

<?php

function convert( $str ) {
    return iconv( "Windows-1252", "UTF-8", $str );
}

if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        echo '<strong>Before Conversion:</strong><br>';
        echo '<pre>' . var_dump($data) . '</pre>';
        $data = array_map( "convert", $data );
        echo '<strong>After Conversion:</strong><br>';
        echo '<pre>' . var_dump($data) . '</pre>';
    }
    fclose($handle);
}

?>

Now since there is no equivalent for iconv in JS, I have tried to run a PHP script with AJAX and get the result.

DEMO in JS(AJAX)

Code: JS:

<script>
    jQuery.get('test.csv', function(data) {
        var storeData = data;
        jQuery.ajax({
            url: "stores-encode.php", 
            type: "POST",
            data: {data: storeData},
            success: function(result){
                document.write(result);
            },error: function(){
                document.write('error');
            }
        }); 
    }); 
</script>

PHP (stores-encode.php):

<?php

$str = $_POST['data'];

$str = iconv( "Windows-1252", "UTF-8", $str );

echo $str;

?>

If you check the demo you can see the strings before and after conversion. I am unable to do the conversion. Can anyone help how can I convert the character sets to include those letters.

You can have a look at the csv file here.

You should change the charset value in test3.php from UTF-8 to Windows-1252 since the charset of the data is Windows-1252.

So you need to replace:

<meta charset="UTF-8"> with <meta charset="Windows-1252">