StrUtil.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. class StrUtil{
  3. /**
  4. * 判断字符串是否为 NULL 或者为 空白字符串 LiaoYun 2018-06-13
  5. * @param str
  6. * @return
  7. */
  8. public static function isEmpOrNull(string $str)
  9. {
  10. if ($str == null || trim($str) == "") {
  11. return true;
  12. } else {
  13. return false;
  14. }
  15. }
  16. public static function isNotEmpAndNull(string $str)
  17. {
  18. if ($str == null || trim($str) == "") {
  19. return false;
  20. } else {
  21. return true;
  22. }
  23. }
  24. public static function isMoblePhone(String $phone) {
  25. if (self::isEmpOrNull($phone)) {
  26. return false;
  27. }
  28. if(!preg_match("/^1[3456789]\d{9}$/",$phone,$res)){
  29. return $res;
  30. } else {
  31. return false;
  32. }
  33. }
  34. public static function back(object $obj, string $funName)
  35. {
  36. $jsonStr = json_encode($obj);
  37. if (self::isNotEmpAndNull($funName)) {
  38. return "<script language='javascript'>parent.{$funName}({$jsonStr})</script>";
  39. } else {
  40. return "<script language='javascript'>parent.callback({$jsonStr})</script>";
  41. }
  42. }
  43. /**
  44. * 中文乱码转 utf-8, 对页面中 用 encodeURI(encodeURI(url)) 处理过的 url 有效
  45. * @return
  46. */
  47. public static function getRequestDecodeParam($request, $key)
  48. {
  49. $str = $request[$key];
  50. if ($str != null) {
  51. try {
  52. //解决%和+转码问题
  53. $str = preg_replace("/%(?![0-9a-fA-F]{2})/", "%25", $str);
  54. $str = preg_replace("/\\+/", "%2B", $str);
  55. return trim(urldecode($str));
  56. } catch (\Exception $e) {
  57. throw new \think\Exception($e->getMessage());
  58. }
  59. } else {
  60. return $str;
  61. }
  62. }
  63. }