123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- /**
- * 把返回的数据集转换成Tree 空数组返回为[]
- * @param array $list 要转换的数据集
- * @param string $pid parent标记字段
- * @param string $level level标记字段
- * @return array
- * @author 麦当苗儿 <zuojiazi@vip.qq.com>
- */
- if (!function_exists('list_to_tree')) {
- function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
- {
- // 创建Tree
- $tree = array();
- if (is_array($list)) {
- // 创建基于主键的数组引用
- $refer = array();
- foreach ($list as $key => $data) {
- $refer[$data[$pk]] =& $list[$key];
- }
- foreach ($list as $key => $data) {
- // 判断是否存在parent
- $parentId = $data[$pid];
- if ($root == $parentId) {
- $tree[] =& $list[$key];
- } else {
- if (isset($refer[$parentId])) {
- $parent =& $refer[$parentId];
- $parent[$child][] =& $list[$key];
- }
- }
- }
- }
- return $tree;
- }
- /**
- * 检测变量是否是手机号码
- * 手机号码必须是11位的数字,第一位数字必须为1,
- * @param string $val 手机号码
- * @return bool
- */
- if (!function_exists('is_mobile')) {
- function is_mobile($val)
- {
- return preg_match('/^1\d{10}$/', $val);
- }
- }
- /**
- * 公共配置数据库连接
- * @param $dbConfig $dbConfig 参数为 id or 模型
- * @return null or dbConfig模型
- */
- function config_connection($dbConfig)
- {
- config(['database.connections.' . $dbConfig['connection_name'] => [
- 'driver' => 'mysql',
- 'host' => $dbConfig['host'],
- 'port' => $dbConfig['port'],
- 'database' => $dbConfig['database'],
- 'username' => $dbConfig['username'],
- 'password' => $dbConfig['password'],
- 'charset' => 'utf8',
- 'prefix' => 'scn_',
- ]]);
- // unset($dbConfig->password);
- return $dbConfig['connection_name'];
- }
- /**
- * 日志记录全局快捷函数
- * @param null $name
- * @return \App\Libs\DoLog
- */
- if (!function_exists('do_log')) {
- function do_log($name = null)
- {
- return new App\Libs\DoLog($name);
- }
- }
- /**
- * 统计单词个数
- * @param $content
- * @return mixed
- */
- function word_count($content)
- {
- $str = strip_tags($content);
- $str = preg_replace('/[\x80-\xff]{1,3}/', ' ', $str, -1, $n);
- $n += str_word_count($str);
- return $n;
- // return str_word_count(strip_tags($content));
- }
- /**
- * 删除图片中的域名
- * @param $content
- * @return null|string|string[]
- */
- function del_domain_from_src($content)
- {
- $url = str_replace('/', '\/', config('app.url')); //去除图片中的域名
- return preg_replace(sprintf('/src="%s/i', $url), 'src="', $content);
- }
- function pre_dump($content)
- {
- echo '<pre>';
- var_dump($content);
- echo '</pre>';
- }
- function encodeStr($string)
- {
- return rtrim(strtr(base64_encode($string), '+/', '-_'), '=');
- }
- function decodeStr($string)
- {
- return base64_decode(strtr($string, '-_', '+/'));
- }
- }
|