I have written the following code in order to read content from an Excel file that contains Japanese characters and display on the web page:
<!DOCTYPE html>
<?php
//header("Content-Type: text/plain; charset=UTF-8"); // output as text file
header("Content-Type: text/html; charset=UTF-8");
if(isset($_POST['upload'])){
unset($_POST['upload']);
$file_name = basename($_FILES['csv_file']['name']);
$name = pathinfo($file_name, PATHINFO_FILENAME );
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$csvFile = fopen($_FILES['csv_file']['tmp_name'], 'r');
//skip first line
fgetcsv($csvFile);
$flag = true; // flag set false when query fails for one or more records
while($line = fgetcsv($csvFile)){
if(count($line)>0){
$data = utf8_decode($line[0]);
echo "$data <br>";
}
}
if($flag)
echo "<h1 style='color:limegreen'> All records imported successfully ! </h1>";
else
echo " Error while fetching one or more records";
fclose($csvFile);
}
?>
<form method="post" action="importExcel.php" enctype="multipart/form-data">
<input type="file" name="csv_file" id="csv_file" accept=".xlsx" >
<input type="submit" name="upload" id="upload" >
</form>
This is the excel sheet with the Japanese characters:
My question is :
- How do i make those Japanese characters to display properly on web browser? I tried using the
utf_decode()
function. It did not help. - Also if i want to store these Japanese alphabets in MySQL database, what changes will i be required to make?
Currently the browser displays the Japanese characters as question marks and some garbage values after using utf_decode()
function. When i remove it, it displays junk values on screen.
Edit: Here is the sample data from the excel file:
アイリッシュ・セッター アイリッシュ・ウォーター・スパニエル アイリッシュ・ウルフハウンド