*/ 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 '
';
        var_dump($content);
        echo '
'; } function encodeStr($string) { return rtrim(strtr(base64_encode($string), '+/', '-_'), '='); } function decodeStr($string) { return base64_decode(strtr($string, '-_', '+/')); } }