📖 File Reader
<?php
namespace app\api\common;
use app\BaseController;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;
/**
* 配置信息
*/
class Common_config extends BaseController
{
/**
* 平台配置信息
* @param $field_name 字段名称(空则查询全部)
*/
static function config($field_name='')
{
$where = 1;
if (!empty($field_name)){
$where .= " and field_name in ($field_name)";
}
$list = db::name("config")->where($where)->field("id,name,field_name,value")->select();
$data = array();//定义数组
foreach ($list as $k => $v){
$data[$v["field_name"]] = $v["value"];
}
return $data;
}
/**
* 二维数组随机出几个
* @param $array 全部数据
* @param $count 随机数量(默认1个)
* @return void
*/
static function getRandomElements($array=[], $count=1)
{
$keys = array_rand($array, $count);
$results = [];
foreach ($keys as $key) {
$results[] = $array[$key];
}
return $results;
}
/**
* 检测是否存在特殊字符
* @param $string 检测字符
* @return bool
*/
static function hasSpecialChars($string) {
// 定义特殊字符的正则表达式
$pattern = '/[\'^£$%&*()}{@#~?><>,|=_+¬-]/';
// 检查字符串中是否有匹配的特殊字符
if (preg_match($pattern, $string)) {
return true; // 存在特殊字符
} else {
return false; // 不存在特殊字符
}
}
/**
* 检测是否为json格式
* @param $string 字符串
* @return bool
*/
static function is_json($string) {
// 使用json_decode解析字符串
$result = json_decode($string);
// 检查json_decode是否失败,失败则表示不是JSON
if ($result === null) {
// 如果json_last_error()不是JSON_ERROR_NONE,则表示解析错误
return json_last_error() === JSON_ERROR_NONE;
} else {
// 如果解析成功,但是$string是一个不正确的JSON,比如一个空字符串或者数字,它会被解析为null
return $result !== null;
}
}
}