StringHelper.php 576 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-07-09 00:59
  7. */
  8. namespace common\helpers;
  9. class StringHelper extends \yii\helpers\StringHelper
  10. {
  11. /**
  12. * encode string with UTF-8
  13. *
  14. * @param $str
  15. * @return string
  16. */
  17. public static function encodingWithUtf8($str)
  18. {
  19. $cur_encoding = mb_detect_encoding($str);
  20. if ($cur_encoding == "UTF-8" && mb_check_encoding($str, "UTF-8")) {
  21. return $str;
  22. } else {
  23. return utf8_encode($str);
  24. }
  25. }
  26. }