使用AJAX将Windows-1252转换为UTF-8 [英] Convert Windows-1252 to UTF-8 with AJAX
问题描述
我在csv文件中有一些带有荷兰字符的字符串,应该用PHP和JS读取.
对于PHP,我正在使用iconv( "Windows-1252", "UTF-8", $str );
方法将Windows-1252
转换为UTF-8
格式.当我使用fgetcsv
和iconv
时,效果很好.
在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:
<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
.
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.
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屋!