php編碼轉換函式(自動轉換字元集支援數群組轉換)

來源:互聯網
上載者:User
  1. // 自動轉換字元集 支援數群組轉換
  2. function auto_charset($fContents, $from='gbk', $to='utf-8') {
  3. $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
  4. $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
  5. if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
  6. //如果編碼相同或者非字串標量則不轉換
  7. return $fContents;
  8. }
  9. if (is_string($fContents)) {
  10. if (function_exists('mb_convert_encoding')) {
  11. return mb_convert_encoding($fContents, $to, $from);
  12. } elseif (function_exists('iconv')) {
  13. return iconv($from, $to, $fContents);
  14. } else {
  15. return $fContents;
  16. }
  17. } elseif (is_array($fContents)) {
  18. foreach ($fContents as $key => $val) {
  19. $_key = auto_charset($key, $from, $to);
  20. $fContents[$_key] = auto_charset($val, $from, $to);
  21. if ($key != $_key)
  22. unset($fContents[$key]);
  23. }
  24. return $fContents;
  25. }
  26. else {
  27. return $fContents;
  28. }
  29. }
複製代碼

這時可能會想到直接用iconv來進行轉碼,但iconv這個函數需要提供的兩個參數為輸入編碼和輸出編碼,而現在根本不知道接受的字串是什麼編碼,如果此時能得到接收字元是什麼編碼就好了。對於這個問題,有以下兩種方案供參考。

方案一要用戶端提交資料時,指定所提交的編碼,這時就需要多給一個用來指定編碼的變數。$string = $_GET['charset'] === 'gbk' ? iconv('gbk','utf-8',$_GET['str']) : $_GET['str'];對於這種情況,如果在沒有約定或者我們不能控制用戶端的情況下,似乎這種方案使用不是很好。

方案二直接由伺服器端來檢測所接收的資料編碼。這種方案當然是最理想了的了,現在問題是怎麼檢測一個字元的編碼嗎?對於這種情況,在php裡,mb_string這個擴充中的mb_check_encoding提供了我們所需要的功能。$str = mb_check_encoding($_GET['str'],'gbk') ? iconv('gbk','utf-8',$_GET['str']) : $_GET['str'];但這需要開啟mb_string這個擴充,有些時候可能我們的生產伺服器中沒有開啟這個擴充。對於這種情況,需要自己藉助如下函數來判斷編碼。

  1. function isGb2312($string) {
  2. for($i=0; $i 127) {
  3. if( ($v >= 228) && ($v < = 233) )
  4. {
  5. if( ($i+2) >= (strlen($string) - 1)) return true;
  6. $v1 = ord( $string[$i+1] );
  7. $v2 = ord( $string[$i+2] );
  8. if( ($v1 >= 128) && ($v1 < =191) && ($v2 >=128) && ($v2 < = 191) )
  9. return false;
  10. else
  11. return true;
  12. }
  13. }
  14. }
  15. return true;
  16. }
  17. function isUtf8($string) {
  18. return preg_match('%^(?:
  19. [\x09\x0A\x0D\x20-\x7E] # ASCII
  20. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  21. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  22. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  23. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  24. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  25. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  26. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  27. )*$%xs', $string);
  28. }
複製代碼

這裡我們就可以使用以上任何一個函數來實現編碼的檢測,並將其轉換為指定的編碼了。$str = isGb2312($_GET['str'],'gbk') ? iconv('gbk','utf-8',$_GET['str']) : $_GET['str'];

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.