ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 1 截取文章里的链接 ```php $pattern = '~[a-z]+://\S+~'; preg_match_all($pattern, $link, $out) if (empty($out[0])) exit('请输入带有链接的文本'); $link = $out[0][0]; ``` ## 2 curl模拟浏览器请求的方法 > 最好使用 GuzzleHttp 插件 ```php composer require guzzlehttp/guzzle ``` ```php $headers = [ 'cookie' => '', 'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36', ]; $client = new Client([ 'verify' => false, 'allow_redirects' => [ 'max' => 0, ], ]); $response = $client->get($link,['headers' => $headers]); $data = $response->getBody()->getContents(); ``` > curl 方式 ```php $cookie = ""; $request_link='';//请求链接 $post_data = '';//请求数据 $referer='';//刷新链接 $agent='';//代理 $header=[];//头部信息 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$request_link); curl_setopt($ch,CURLOPT_HTTPHEADER,$header); curl_setopt($ch, CURLOPT_COOKIE, $cookie); curl_setopt($ch, CURLOPT_REFERER,$referer); curl_setopt($ch,CURLOPT_USERAGENT,$agent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch); curl_close($ch); return $output; ``` ## 3 通过域名获取IP的方法 ```php gethostbyname($host) ``` ## 4 随机生成字符 ```php $chars = [ 1 => '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 2 => '123456789', 3 => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|', 4 => 'abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ]; $str = ''; for ($i = 0; $i < $length; $i++) { $str .= $chars[$type][mt_rand(0, strlen($chars[$type]) - 1)]; } return $str; ``` ## 5 随机生成订单号 ```php $y = date('y'); $m=date('m'); $d=date('d'); $h=date('h'); $i=date('i'); $s=date('s'); $date=ceil($y+$m+$d).ceil($h+$i+$s); $digits = intval($digits); if ($digits < 14) return '随机单号大于14字符'; $digits = $digits - strlen($date) - strlen($prefix); if ($digits < 0) return '随机单号生成失败'; $rand = ''; $num = floor($digits / 10); for ($i = 0; $i < $num; $i++) { $rand .= str_pad(mt_rand(1, (int)9999999999), 10, '0', STR_PAD_LEFT); } if ($digits % 10 != 0) { $rand .= str_pad(mt_rand(1, (int)substr(9999999999, 0, $digits - ($num * 10))), $digits - ($num * 10), '0', STR_PAD_LEFT); } $order_number = $prefix . $date . $rand; return $order_number; ``` ## 6 截取字符串,两个字符中间的内容 ```php $string = ' ' . $string; $ini = strpos($string, $start); if ($ini == 0) return ''; $ini += strlen($start); $len = is_null($end)?strlen($string)-$ini:strpos($string, $end, $ini) - $ini; return substr($string, $ini, $len); ```