💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[toc] ## :-: **打印** ```php /* 打印数组或字符串 * @param string $value 要打印的变量 * @param int $now 为1是去掉exit的功能 */ function o($value='',$now=0){ if($now==0) { echo '<pre>'; print_r($value); echo '</pre>'; exit; } elseif($now==1) { echo '<pre>'; print_r($value); echo '</pre>'; } ``` ## :-: **检查ip格式 对不对** ```php //若IP格式不对,则返回false。否则返回true function filter_ip($ip){ if (!filter_var($ip, FILTER_VALIDATE_IP)) return false; return true; } ``` ## :-: **检测域名** ```php /** * 检测域名 */ function checkDomain2($domain){ if(preg_match('/^(http:\/\/|https:\/\/)+([0-9a-zA-Z\.\-\_]{1,32})+(\.[a-zA-Z]{2,5})$/', $domain) || preg_match('/^(http:\/\/|https:\/\/)+((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/', $domain) || preg_match('/^(http:\/\/|https:\/\/)+([0-9a-zA-Z\.\-\_]{1,32})+(\.[a-zA-Z]{2,5}):\d{0,5}$/', $domain) || preg_match('/^(http:\/\/|https:\/\/)+((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d)))):\d{0,5}$/', $domain) || preg_match('/^(http:\/\/|https:\/\/)+(.*)$/', $domain) ) { return true; }else{ return false; } } ``` ## :-: **检测ip** ```php /** * 检测IP */ function checkIp($ip){ //ipv6 $preg = "/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-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|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*)$/"; if(preg_match('/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/', $ip) || preg_match('/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))-((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/', $ip) || preg_match($preg,$ip) || preg_match($preg2,$ip)) { return true; }else{ return false; } } ``` ## :-: **xml 转 数组** ```php /** * 作用:将xml转为array * @param $xml * @return mixed */ function xmlToArray($xml) { //将XML转为array $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $array_data; } ``` ## :-: **对象 转 数组** ```php //将对象转成数组 function objectToArray($obj){ $obj = (array)$obj; foreach ($obj as $k => $v) { if (gettype($v) == 'resource') { return; } if (gettype($v) == 'object' || gettype($v) == 'array') { $obj[$k] = (array)object_to_array($v); } } return $obj; } ``` ## :-: **ip地址 转 整数** ```php /* * 把ip地址转换成整数 * 效果同ip2long此函数 */ function ipToInt($ip){ $iparr = explode('.',$ip); $num = 0; for($i=0;$i<count($iparr);$i++){ $num += intval($iparr[$i]) * pow(256,count($iparr)-($i+1)); } return $num; } ``` 调用示例: ![](https://box.kancloud.cn/297c08affc656ca28c21a1336bb1fb08_530x113.png) ## :-: **curl 发 get 或 post 请求** ```php //curl发送get或post请求 function http_request($url,$data=null){ $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); if($data){ curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$data); } curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $output = curl_exec($ch); curl_close($ch); return $output; } ``` ## :-: **无限极分类** ```php /*无限极分类*/ function getTree(&$list,$pid=0,$level=0){ static $tree = array(); foreach($list as $v){ if($v['parent'] == $pid){ $tmp = array( 'id'=>$v['id'], 'parent'=>$v['parent'], 'name'=>$v['name'], 'level'=>$level ); $tree[] = $tmp; getTree($list,$v['id'],$level+1); } } return $tree; } ``` ## :-: **随机秘钥** ```php /** * 生成(大小写+数字的)随机秘钥 * @param int $num 要生成几位的 * @return bool|string */ function randomStr($num = 6){ $str = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm"; str_shuffle($str); //随机打乱一个字符串 $name = substr(str_shuffle($str), 0, $num); return $name; } ```