2012-04-20 22 views
1

我試圖出口從數據庫中的某些信息,但在創建CSV文件時的特殊字符,像這樣Categoría如何將CSV導出爲特殊字符?

這是我使用導出

function exportemail() { 
global $wpdb; 
    $content = "No.,Nombre,Empresa,Categoría,Estado de Prospecto,Correo,Teléfono,Dirección,Referido por,País,Sitio web,Contacto Alternativo,Trabajo de Contacto Alternativo,Correo de Contacto Alternativo,Teléfono de Contacto Alternativo,Dirección de Contacto Alternativo,País de Contacto Alternativo,Sitio Web de Contacto Alternativo,Notas,Rung"; 

    if(trim($_GET['tab']) != "") 
    { 
     $sql = "SELECT * FROM ".$wpdb->prefix."crm WHERE 
       category LIKE '%".$wpdb->escape($_GET['tab'])."%' 
       ORDER BY name"; 
    } 
    elseif (trim($searcharea) != '') { 
     $sql = "SELECT * FROM ".$wpdb->prefix."crm WHERE 
      name LIKE '%".$wpdb->escape($_GET['q'])."%' 
      OR last_name LIKE '%".$wpdb->escape($_GET['q'])."%' 
      OR category LIKE '%".$wpdb->escape($_GET['q'])."%' 
      OR business LIKE '%".$wpdb->escape($_GET['q'])."%' 
      OR email LIKE '%".$wpdb->escape($_GET['q'])."%' 
      OR phone LIKE '%".$wpdb->escape($_GET['q'])."%' 
      OR notes LIKE '%".$wpdb->escape($_GET['q'])."%' 
      OR rung LIKE '%".$wpdb->escape($_GET['q'])."%' 
      ORDER BY name"; 
    } else { 
     $sql = "SELECT * FROM ".$wpdb->prefix."crm ORDER BY name"; 
    } 

    $results = $wpdb->get_results($sql); 
     $c = 1; 
     foreach ($results as $row) { 


     $content .= "\n".$c.",".str_replace(","," -", 
     $row->name." ". 
     $row->last_name).",".str_replace(","," -", 
     $row->business).",".str_replace(","," -", 
     $row->category).",".str_replace(","," -", 
     $row->rank).",".str_replace(","," -", 
     $row->email).",".str_replace(","," -", 
     $row->phone).",".str_replace(","," -", 
     $row->address).",".str_replace(","," -", 
     $row->referral).",".str_replace(","," -", 
     $row->country).",".str_replace(","," -", 
     $row->website).",".str_replace(","," -", 
     $row->altern_last_name." ". 
     $row->altern_last_name).",".str_replace(","," -", 
     $row->altern_business).",".str_replace(","," -", 
     $row->altern_email).",".str_replace(","," -", 
     $row->altern_phone).",".str_replace(","," -", 
     $row->altern_address)." ".str_replace(","," -", 
     $row->altern_country).",".str_replace(","," -", 
     $row->altern_website).",".str_replace(","," -", 
     $row->notes).",".str_replace(","," -", 
     $row->rung); 

     $c++; 
    } 


    $file = "../wp-content/plugins/email.csv"; 
    chmod($file,0777); 
    $fp = fopen($file,'w'); 
    fwrite($fp,$content); 
    fclose($fp); 
    if (file_exists($file)) { 
     header('Content-Description: File Transfer'); 
     header('Content-Encoding: UTF-8'); 
     header('Content-type: text/csv; charset=UTF-8'); 
     header('Content-Disposition: attachment; filename='.basename($file)); 
     echo "\xEF\xBB\xBF"; // UTF-8 BOM 
     header('Content-Transfer-Encoding: binary'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header('Pragma: public'); 
     header('Content-Length: ' . filesize($file)); 
     ob_clean(); 
     flush(); 
     readfile($file); 
     unlink($file); 
     exit; 
    } 

}

代碼

我一直在嘗試使用htmlspecialchars來解決這個問題,但它不工作,我不知道如果問題是我把它放錯了,或者如果它是別的東西...因爲我不是專家,我仍然一樣,任何人都可以幫助這個嗎?

+0

http://stackoverflow.com/questions/1221835/convert-utf-16-hex前-to-utf-8-in-php我之前遇到過這個問題,這個答案可能對你有幫助。 – 2012-04-20 17:02:44

回答

11

我可以看到你已經包含了神奇的字節順序標記(BOM)。不幸的是,它不是內容的一部分。

走線

echo "\xEF\xBB\xBF"; // UTF-8 BOM 

,並把它只是在你的ReadFile()

... 
echo "\xEF\xBB\xBF"; // UTF-8 BOM 
readfile($file); 
... 
+0

當你知道,你知道。非常感謝這個幫助 – Gman 2012-04-20 22:14:21