function.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * 下划线转驼峰
  4. * 思路:
  5. * step1.原字符串转小写,原字符串中的分隔符用空格替换,在字符串开头加上分隔符
  6. * step2.将字符串中每个单词的首字母转换为大写,再去空格,去字符串首部附加的分隔符.
  7. */
  8. function camelize($uncamelized_words, $separator='_')
  9. {
  10. $uncamelized_words = $separator. str_replace($separator, " ", strtolower($uncamelized_words));
  11. return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator );
  12. }
  13. /**
  14. * 驼峰命名转下划线命名
  15. * 思路:
  16. * 小写和大写紧挨一起的地方,加上分隔符,然后全部转小写
  17. */
  18. function uncamelize($camelCaps, $separator='_')
  19. {
  20. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
  21. }
  22. /**
  23. * 防sql注入字符串转义
  24. * @param $content 要转义内容
  25. * @return array|string
  26. */
  27. function escapeString($content)
  28. {
  29. $pattern = "/(select[\s])|(insert[\s])|(update[\s])|(delete[\s])|(from[\s])|(where[\s])|(drop[\s])/i";
  30. if (is_array($content)) {
  31. foreach ($content as $key => $value) {
  32. $content[$key] = addslashes(trim($value));
  33. if (preg_match($pattern, $content[$key])) {
  34. // echo $content[$key];
  35. $content[$key] = '';
  36. }
  37. }
  38. } else {
  39. $content = addslashes(trim($content));
  40. if (preg_match($pattern, $content)) {
  41. $content = '';
  42. }
  43. }
  44. return $content;
  45. }
  46. /**
  47. * json 格式输出对象
  48. */
  49. function print_json($data)
  50. {
  51. $res = json_encode($data, JSON_PRETTY_PRINT);
  52. if (IS_CLI) {
  53. echo $res;
  54. } else {
  55. // 替换空格和换行符
  56. $res = str_replace(' ', '&nbsp;', $res);
  57. $res = str_replace("\n", '<br>', $res);
  58. echo $res;
  59. }
  60. }