使用AJAX将Windows-1252转换为UTF-8 [英] Convert Windows-1252 to UTF-8 with AJAX

查看:132
本文介绍了使用AJAX将Windows-1252转换为UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在csv文件中有一些带有荷兰字符的字符串,应该用PHP和JS读取.

对于PHP,我正在使用iconv( "Windows-1252", "UTF-8", $str );方法将Windows-1252转换为UTF-8格式.当我使用fgetcsviconv时,效果很好.

在PHP中演示

在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);
}

?>

现在,由于JS中没有iconv的等效项,我尝试使用AJAX运行PHP脚本并获得结果.

JS(AJAX)中的DEMO

代码: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;

?>

如果您查看演示,则可以看到转换前后的字符串.我无法进行转换.任何人都可以帮助我如何将字符集转换为包括这些字母.

您可以在此处查看csv文件.

解决方案

由于数据的字符集是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">

这篇关于使用AJAX将Windows-1252转换为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆