helps.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * 把返回的数据集转换成Tree 空数组返回为[]
  4. * @param array $list 要转换的数据集
  5. * @param string $pid parent标记字段
  6. * @param string $level level标记字段
  7. * @return array
  8. * @author 麦当苗儿 <zuojiazi@vip.qq.com>
  9. */
  10. if (!function_exists('list_to_tree')) {
  11. function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
  12. {
  13. // 创建Tree
  14. $tree = array();
  15. if (is_array($list)) {
  16. // 创建基于主键的数组引用
  17. $refer = array();
  18. foreach ($list as $key => $data) {
  19. $refer[$data[$pk]] =& $list[$key];
  20. }
  21. foreach ($list as $key => $data) {
  22. // 判断是否存在parent
  23. $parentId = $data[$pid];
  24. if ($root == $parentId) {
  25. $tree[] =& $list[$key];
  26. } else {
  27. if (isset($refer[$parentId])) {
  28. $parent =& $refer[$parentId];
  29. $parent[$child][] =& $list[$key];
  30. }
  31. }
  32. }
  33. }
  34. return $tree;
  35. }
  36. /**
  37. * 检测变量是否是手机号码
  38. * 手机号码必须是11位的数字,第一位数字必须为1,
  39. * @param string $val 手机号码
  40. * @return bool
  41. */
  42. if (!function_exists('is_mobile')) {
  43. function is_mobile($val)
  44. {
  45. return preg_match('/^1\d{10}$/', $val);
  46. }
  47. }
  48. /**
  49. * 公共配置数据库连接
  50. * @param $dbConfig $dbConfig 参数为 id or 模型
  51. * @return null or dbConfig模型
  52. */
  53. function config_connection($dbConfig)
  54. {
  55. config(['database.connections.' . $dbConfig['connection_name'] => [
  56. 'driver' => 'mysql',
  57. 'host' => $dbConfig['host'],
  58. 'port' => $dbConfig['port'],
  59. 'database' => $dbConfig['database'],
  60. 'username' => $dbConfig['username'],
  61. 'password' => $dbConfig['password'],
  62. 'charset' => 'utf8',
  63. 'prefix' => 'scn_',
  64. ]]);
  65. // unset($dbConfig->password);
  66. return $dbConfig['connection_name'];
  67. }
  68. /**
  69. * 日志记录全局快捷函数
  70. * @param null $name
  71. * @return \App\Libs\DoLog
  72. */
  73. if (!function_exists('do_log')) {
  74. function do_log($name = null)
  75. {
  76. return new App\Libs\DoLog($name);
  77. }
  78. }
  79. /**
  80. * 统计单词个数
  81. * @param $content
  82. * @return mixed
  83. */
  84. function word_count($content)
  85. {
  86. $str = strip_tags($content);
  87. $str = preg_replace('/[\x80-\xff]{1,3}/', ' ', $str, -1, $n);
  88. $n += str_word_count($str);
  89. return $n;
  90. // return str_word_count(strip_tags($content));
  91. }
  92. /**
  93. * 删除图片中的域名
  94. * @param $content
  95. * @return null|string|string[]
  96. */
  97. function del_domain_from_src($content)
  98. {
  99. $url = str_replace('/', '\/', config('app.url')); //去除图片中的域名
  100. return preg_replace(sprintf('/src="%s/i', $url), 'src="', $content);
  101. }
  102. function pre_dump($content)
  103. {
  104. echo '<pre>';
  105. var_dump($content);
  106. echo '</pre>';
  107. }
  108. function encodeStr($string)
  109. {
  110. return rtrim(strtr(base64_encode($string), '+/', '-_'), '=');
  111. }
  112. function decodeStr($string)
  113. {
  114. return base64_decode(strtr($string, '-_', '+/'));
  115. }
  116. }