php判斷字符串是否包含某個字符串,PHP 判斷字符的編碼 并輸出想要的編碼格式字符 (轉)

 2023-12-06 阅读 25 评论 0

摘要:[php] view plain copy print? /** * 判斷字符編碼? 并輸出想要的編碼 * Enter description here ... * @param unknown_type $string * @param unknown_type $outEncoding */? function safeEncoding($string,$outEncoding ='UTF-8') {? ??????? $encod
[php] view plain copy print?
  1. /**
  2. * 判斷字符編碼? 并輸出想要的編碼
  3. * Enter description here ...
  4. * @param unknown_type $string
  5. * @param unknown_type $outEncoding
  6. */?
  7. function safeEncoding($string,$outEncoding ='UTF-8') {?
  8. ??????? $encoding = "UTF-8";?
  9. ??????? for($i=0;$i<strlen($string);$i++){?
  10. ??????????? if(ord($string{$i})<128)?
  11. ??????????? continue;?
  12. ??????????? if((ord($string{$i})&224)==224){?
  13. ??????????????? //第一個字節判斷通過?
  14. ??????????????? $char = $string{++$i};?
  15. ??????????????? if((ord($char)&128)==128){?
  16. ??????????????????? //第二個字節判斷通過?
  17. ??????????????????? $char = $string{++$i};?
  18. ??????????????????? if((ord($char)&128)==128)?
  19. ??????????????????? {?
  20. ??????????????????????? $encoding = "UTF-8";?
  21. ??????????????????????? break;?
  22. ??????????????????? }?
  23. ??????????????? }?
  24. ??????????? }?
  25. ??????????? if((ord($string{$i})&192)==192){?
  26. ??????????????? //第一個字節判斷通過?
  27. ??????????????? $char = $string{++$i};?
  28. ??????????????? if((ord($char)&128)==128){?
  29. ??????????????????? // 第二個字節判斷通過?
  30. ??????????????????? $encoding = "GB2312";?
  31. ??????????????????? break;?
  32. ??????????????? }?
  33. ??????????? }?
  34. ??????? }?
  35. ??????????
  36. ??????? if(strtoupper($encoding) == strtoupper($outEncoding))?
  37. ??????? return $string;?
  38. ??????? else?
  39. ??????? return iconv($encoding,$outEncoding,$string);?
  40. ??? }?
  41. http://blog.csdn.net/yw5201314/article/details/8665099

轉載于:https://www.cnblogs.com/xihong2014/p/5337075.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/192210.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息