簡體   English   中英

PHP MySQL中的UTF8

[英]UTF8 in php mysql

我正在嘗試通過php將數據從xls導入mysql。 我在通過它保存UTF-8文本時遇到問題。 我將其另存為???????。 我的數據庫表結構是utf8_general_ci以及我的php代碼如下

  <?php $con = mysqli_connect('localhost', 'myuser', 'mypass', 'mydb'); if(isset($_POST["submit"])) { mysqli_query($con,'SET character_set_results=utf8'); mysqli_query($con,'SET names=utf8'); mysqli_query($con,'SET character_set_client=utf8'); mysqli_query($con,'SET character_set_connection=utf8'); mysqli_query($con,'SET character_set_results=utf8'); mysqli_query($con,'SET collation_connection=utf8_general_ci'); $file = $_FILES['file']['tmp_name']; $handle = fopen($file, "r"); $i = 0; while(($filesop = fgetcsv($handle, 1000, ",")) !== false) { $option1 = $filesop[0]; $option2 = $filesop[1]; $option3 = $filesop[2]; $option4 = $filesop[3]; $correctans = $filesop[4]; $question_text = $filesop[5]; $cat_id = $filesop[6]; $sub_cat_id = $filesop[7]; $level_id = $filesop[8]; $quesimage = $filesop[9]; $sql = mysqli_query($con,"INSERT IGNORE INTO questions (option1, option2,option3,option4,correctans,question_text,cat_id,sub_cat_id,level_id,quesimage) VALUES ('".$option1."','".$option2."','".$option3."','".$option4."','".$correctans."','".$question_text."','".$cat_id."','".$sub_cat_id."','".$level_id."','".$quesimage."')"); $i = $i + 1; } //echo $sql; if($sql) { echo "You database has imported successfully. You have inserted ". $i ." records"; } else { echo "Sorry!"; } } ?> <html> <head> <title>Import Questions</title> </head> <body> <form method="post" action="" enctype="multipart/form-data"> Upload Excel File : <input type="file" name="file" /><br /> <input type="submit" name="submit" value="Upload" /> </form> </body> </html> 

它的英文文本工作正常,但印地語或古吉拉特語文本卻出現問題 我該如何解決?

謝謝

請注意,在使用fgetcsv()函數讀取數據時,將考慮locale設置。 如果LANG是例如en_US.UTF-8 ,則此功能將錯誤讀取一字節編碼的文件。

您可以嘗試另一件事-即時轉換.csv文檔(將UCS-2更改為您的文件編碼):

function parse_csv($filename) {
    if (($handle != fopen($filename, "r"))) return false;
    while (($cols = fgetcsv($handle, 1000, "\t")) !== FALSE) {
        foreach( $cols as $key => $value ) {
            $cols[$key] = trim( $cols[$key] );
            $cols[$key] = iconv('UCS-2', 'UTF-8', $cols[$key]."\0") ;
            $cols[$key] = str_replace('""', '"', $cols[$key]);
            $cols[$key] = preg_replace("/^\"(.*)\"$/sim", "$1", $cols[$key]);
        }
        echo var_dump($cols); //This will display an array of your data
    }
}

使用與上一篇文章相同的思想,但進行一些修改:

function global_client_charset($charset){
if(!isset($charset)){
    $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    if(strrpos($user_agent,"linux")){
    $GLOBALS["CHARSET"] = "UTF-8";
    }else if(strrpos($user_agent,"windows")){
    $GLOBALS["CHARSET"] = "ISO-8859-1";
    }
}else{
        $GLOBALS["CHARSET"] = $charset;
}       
}

function toUTF8($data){
    if($GLOBALS["CHARSET"] === "ISO-8859-1"){
        return iconv("ISO-8859-1", "UTF-8", trim($data));
    }else if($GLOBALS["CHARSET"] === "UTF-8"){
       return trim($data);
    }else{
       return trim($data);
    }
}

if(isset($_POST["submit"]))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$i = 0;

//do you know the charset you are receiving ??
//global_client_charset("ISO-8859-1");
global_client_charset();

while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{   
    $option1 = $filesop[0];
    $option2 = $filesop[1];
    $option3 = $filesop[2];
    $option4 = $filesop[3];
    $correctans = $filesop[4];
    $question_text = $filesop[5];
    $cat_id = $filesop[6];
    $sub_cat_id = $filesop[7];
    $level_id = $filesop[8];
    $quesimage = $filesop[9];


    $query = "INSERT IGNORE INTO questions (option1, option2,option3,option4,correctans,question_text,cat_id,sub_cat_id,level_id,quesimage) VALUES ('".
    $option1."','".$option2."','".$option3."','".$option4."','".$correctans."','".$question_text."','".
    $cat_id."','".$sub_cat_id."','".$level_id."','".$quesimage."')"; 

    //echo toUTF8($query); die();
    $sql = mysqli_query($con,toUTF8($query));
    $i = $i + 1;        
}

if($sql)
{
    echo "You database has imported successfully. You have inserted ". $i ." records";
}
else
{
    echo "Sorry!";
}
}
?>
<html>
<head>
<title>Import Questions</title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
Upload Excel File : <input type="file" name="file" /><br />
<input type="submit" name="submit" value="Upload" />
</form> 
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM