php程式對微信你暱稱的表情處理匯出excel檔案

与f發表於2024-08-10

php程式對微信暱稱的表情處理,若轉義存資料庫,怎麼讀出並匯出成excel檔案.

<?php

include(dirname(__FILE__) .'/phpexcel-1.7.7/Classes/PHPExcel.php');
include(dirname(__FILE__) .'/phpexcel-1.7.7/Classes/PHPExcel/IOFactory.php');

    $name2 = "[[EMOJI:%F0%9F%91%BF]]李[[EMOJI:%F0%9F%8C%B9]]李[[EMOJI:%F0%9F%8D%92]]李[[EMOJI:%F0%9F%90%B0]] [[EMOJI:%F0%9F%87%A8]] [[EMOJI:%F0%9F%87%B3]] ";
    $name2_emoji = emoji_decode($name2);
    $name1_emoji = emoji_decode("漢字");
    //var_dump($name2_emoji);
    //var_dump($name1_emoji);exit;

    $objPHPExcel = new PHPExcel();
    //在啟用的工作區寫入資料 (單條寫入資料演示)
    $objPHPExcel->getActiveSheet()->setCellValue('A1', $name2_emoji);
    $objPHPExcel->getActiveSheet()->setCellValue('A2', $name1_emoji);
    $objPHPExcel->getActiveSheet()->setCellValue('A3', "aa😓哈茲😀 茲");
    $objPHPExcel->getActiveSheet()->setCellValue('A4', "aa👏哈茲😀 茲");
    $objPHPExcel->getActiveSheet()->setCellValue('B1', "aa😓哈茲😀 茲");
    $objPHPExcel->getActiveSheet()->setCellValue('B2', "aa😓哈茲😀 茲");
    $objPHPExcel->getActiveSheet()->setCellValue('B3', "aa👏哈茲😀 茲");
    $objPHPExcel->getActiveSheet()->setCellValue('B4', "1321545 😀 茲");

    //寫資料結束
    //$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
    //$objWriter->save('php://output');
 
    // 將輸出推到前端
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="my_excel_sheet.xlsx"');
    header('Cache-Control: max-age=0');
     
    // 如果你想要開啟IE的下載對話方塊,而不是直接下載
    // header('Content-Disposition: inline; filename="my_excel_sheet.xlsx"');
     
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output'); 
    // 確保所有的輸出被髮送到客戶端
    ob_end_flush();
    flush();
    exit();
    
//對emoji表情轉反義
function emoji_decode($str)
{
    $strDecode = preg_replace_callback('|\[\[EMOJI:(.*?)\]\]|', function ($matches) {
        return rawurldecode($matches[1]);
    }, $str);
    return $strDecode;
}


/**對emoji表情轉義
 * @param $nickname
 * @return string
 */
function emoji_encode($nickname)
{
    $strEncode = '';
    $length = mb_strlen($nickname, 'utf-8');
    for ($i = 0; $i < $length; $i++) {
        $_tmpStr = mb_substr($nickname, $i, 1, 'utf-8');
        if (strlen($_tmpStr) >= 4) {
            $strEncode .= '[[EMOJI:' . rawurlencode($_tmpStr) . ']]';
        } else {
            $strEncode .= $_tmpStr;
        }
    }
    return $strEncode;
}
?>

相關文章