StrUtil.php 2.0 KB

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