Reputation: 4956
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.
Upvotes: 1
Views: 3348
Reputation: 3773
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">
Upvotes: 1