common.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. if (!function_exists('exception')) {
  13. /**
  14. * 抛出异常处理
  15. *
  16. * @param string $msg 异常消息
  17. * @param integer $code 异常代码 默认为0
  18. * @param string $exception 异常类
  19. *
  20. * @throws Exception
  21. */
  22. function exception($msg, $code = 0, $exception = '')
  23. {
  24. $e = $exception ?: '\think\Exception';
  25. throw new $e($msg, $code);
  26. }
  27. }
  28. if (!function_exists('filterEmoji')) {
  29. // 过滤掉emoji表情
  30. function filterEmoji($str)
  31. {
  32. $str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
  33. '/./u',
  34. function (array $match) {
  35. return strlen($match[0]) >= 4 ? '' : $match[0];
  36. },
  37. $str);
  38. return $str;
  39. }
  40. }
  41. if (!function_exists('strReplace')) {
  42. /**
  43. * @param string $string 需要替换的字符串
  44. * @param int $start 开始的保留几位
  45. * @param int $end 最后保留几位
  46. * @return string
  47. */
  48. function strReplace($string, $start, $end)
  49. {
  50. $strlen = mb_strlen($string, 'UTF-8');//获取字符串长度
  51. $firstStr = mb_substr($string, 0, $start, 'UTF-8');//获取第一位
  52. $lastStr = mb_substr($string, -1, $end, 'UTF-8');//获取最后一位
  53. return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($string, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  54. }
  55. }
  56. if (!function_exists('sensitive_words_filter')) {
  57. /**
  58. * 敏感词过滤
  59. *
  60. * @param string
  61. * @return string
  62. */
  63. function sensitive_words_filter($str)
  64. {
  65. if (!$str) return '';
  66. $file = app()->getAppPath() . 'public/static/plug/censorwords/CensorWords';
  67. $words = file($file);
  68. foreach ($words as $word) {
  69. $word = str_replace(array("\r\n", "\r", "\n", "/", "<", ">", "=", " "), '', $word);
  70. if (!$word) continue;
  71. $ret = preg_match("/$word/", $str, $match);
  72. if ($ret) {
  73. return $match[0];
  74. }
  75. }
  76. return '';
  77. }
  78. }
  79. if (!function_exists('make_path')) {
  80. /**
  81. * 上传路径转化,默认路径
  82. * $type 类型
  83. */
  84. function make_path($path,$type = 2)
  85. {
  86. $path = DS.ltrim(rtrim($path));
  87. switch ($type){
  88. case 1:
  89. $path .= DS.date('Y');
  90. break;
  91. case 2:
  92. $path .= DS.date('Y').DS.date('m');
  93. break;
  94. case 3:
  95. $path .= DS.date('Y').DS.date('m').DS.date('d');
  96. break;
  97. }
  98. try{
  99. if (is_dir(app()->getRootPath().'public'.DS.'uploads'.$path) == true || mkdir(app()->getRootPath().'public'.DS.'uploads'.$path, 0777, true) == true) {
  100. return trim(str_replace(DS, '/',$path),'.');
  101. }else return '';
  102. }catch (\Exception $e){
  103. return '无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS. 'attach' . DS;
  104. }
  105. }
  106. }