企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 自定义函数Fn 测试变量 ``` <pre class="calibre14">``` <span class="token5">if</span> <span class="token2">(</span><span class="token">!</span><span class="token1">function_exists</span><span class="token2">(</span><span class="token4">'dump'</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">function</span> <span class="token1">dump</span><span class="token2">(</span>$arr<span class="token2">)</span><span class="token2">{</span> echo <span class="token4">''</span><span class="token2">.</span><span class="token1">print_r</span><span class="token2">(</span>$arr<span class="token2">,</span>TRUE<span class="token2">)</span><span class="token2">.</span><span class="token4">''</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` 查找子字符串 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">findStr</span><span class="token2">(</span>$string<span class="token2">,</span> $find<span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token">!</span><span class="token2">(</span><span class="token1">strpos</span><span class="token2">(</span>$string<span class="token2">,</span> $find<span class="token2">)</span> <span class="token">===</span> FALSE<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` 多维数组转换成一维数组 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">_arr</span><span class="token2">(</span>$array<span class="token2">,</span>$<span class="token5">return</span><span class="token">=</span><span class="token2">[</span><span class="token2">]</span><span class="token2">)</span><span class="token2">{</span> <span class="token1">array_walk_recursive</span><span class="token2">(</span>$array<span class="token2">,</span><span class="token5">function</span><span class="token2">(</span>$value<span class="token2">)</span><span class="token1">use</span><span class="token2">(</span><span class="token">&</span>$<span class="token5">return</span><span class="token2">)</span><span class="token2">{</span>$<span class="token5">return</span><span class="token2">[</span><span class="token2">]</span><span class="token">=</span>$value<span class="token2">;</span><span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> $<span class="token5">return</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` emoji转换解码 de ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">emoji</span><span class="token2">(</span>$str<span class="token2">,</span> $is <span class="token">=</span> <span class="token4">'en'</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">if</span> <span class="token2">(</span><span class="token4">'en'</span> <span class="token">==</span> $is<span class="token2">)</span> <span class="token2">{</span> <span class="token5">if</span> <span class="token2">(</span><span class="token">!</span><span class="token1">is_string</span><span class="token2">(</span>$str<span class="token2">)</span><span class="token2">)</span> <span class="token5">return</span> $str<span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span><span class="token">!</span>$str <span class="token">||</span> $str <span class="token">==</span> <span class="token4">'undefined'</span><span class="token2">)</span> <span class="token5">return</span> <span class="token4">''</span><span class="token2">;</span> $text <span class="token">=</span> <span class="token1">json_encode</span><span class="token2">(</span>$str<span class="token2">)</span><span class="token2">;</span> $text <span class="token">=</span> <span class="token1">preg_replace_callback</span><span class="token2">(</span><span class="token4">"/(\\\u[ed][0-9a-f]{3})/i"</span><span class="token2">,</span> <span class="token5">function</span> <span class="token2">(</span>$str<span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token1">addslashes</span><span class="token2">(</span>$str<span class="token2">[</span><span class="token3">0</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">,</span> $text<span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> <span class="token1">json_decode</span><span class="token2">(</span>$text<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> $text <span class="token">=</span> <span class="token1">json_encode</span><span class="token2">(</span>$str<span class="token2">)</span><span class="token2">;</span> $text <span class="token">=</span> <span class="token1">preg_replace_callback</span><span class="token2">(</span><span class="token4">'/\\\\\\\\/i'</span><span class="token2">,</span> <span class="token5">function</span> <span class="token2">(</span>$str<span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token4">'\\'</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">,</span> $text<span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> <span class="token1">json_decode</span><span class="token2">(</span>$text<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` 去除数组值两端空格 function trimarr($Input){ if (!is\_array($Input)) return trim($Input); return array\_map('trimarr', $Input); } /\*\* - 时间友好显示,传入时间戳 \*/ ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">timeline</span><span class="token2">(</span>$time<span class="token2">)</span> <span class="token2">{</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">time</span><span class="token2">(</span><span class="token2">)</span> <span class="token"><=</span> $time<span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token1">date</span><span class="token2">(</span><span class="token4">"Y-m-d H:i:s"</span><span class="token2">,</span> $time<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> $t <span class="token">=</span> <span class="token1">time</span><span class="token2">(</span><span class="token2">)</span> <span class="token">-</span> $time<span class="token2">;</span> $f <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span> <span class="token4">'31536000'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'年'</span><span class="token2">,</span> <span class="token4">'2592000'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'个月'</span><span class="token2">,</span> <span class="token4">'604800'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'星期'</span><span class="token2">,</span> <span class="token4">'86400'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'天'</span><span class="token2">,</span> <span class="token4">'3600'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'小时'</span><span class="token2">,</span> <span class="token4">'60'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'分钟'</span><span class="token2">,</span> <span class="token4">'1'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'秒'</span> <span class="token2">)</span><span class="token2">;</span> foreach <span class="token2">(</span>$f as $k <span class="token">=</span><span class="token">></span> $v<span class="token2">)</span> <span class="token2">{</span> <span class="token5">if</span> <span class="token2">(</span><span class="token3">0</span> <span class="token">!=</span> $c <span class="token">=</span> <span class="token1">floor</span><span class="token2">(</span>$t <span class="token">/</span> <span class="token2">(</span>int<span class="token2">)</span>$k<span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> $c <span class="token2">.</span> $v <span class="token2">.</span> <span class="token4">'前'</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` 获取文件扩展名 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">fileExt</span><span class="token2">(</span>$file<span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token1">strtolower</span><span class="token2">(</span><span class="token1">pathinfo</span><span class="token2">(</span>$file<span class="token2">,</span> <span class="token3">4</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` /\*\* - post提交 - @param $url - @param $msg - @return mixed \*/ ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">post</span><span class="token2">(</span>$url<span class="token2">,</span> $msg<span class="token2">)</span> <span class="token2">{</span><span class="token6">//post ssl</span> $ch <span class="token">=</span> <span class="token1">curl_init</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">class_exists</span><span class="token2">(</span><span class="token4">'\CURLFile'</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_SAFE_UPLOAD<span class="token2">,</span> <span class="token3">true</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">defined</span><span class="token2">(</span><span class="token4">'CURLOPT_SAFE_UPLOAD'</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_SAFE_UPLOAD<span class="token2">,</span> <span class="token3">false</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token6">//$msg = array('media'=>"@".$filepath);</span> <span class="token6">//5.6+ $msg = array('media'=>new \CURLFile($filepath));</span> <span class="token1">preg_match</span><span class="token2">(</span><span class="token4">'/https:\/\//'</span><span class="token2">,</span> $url<span class="token2">)</span> <span class="token">?</span> $ssl <span class="token">=</span> TRUE <span class="token2">:</span> $ssl <span class="token">=</span> FALSE<span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_POST<span class="token2">,</span> <span class="token3">1</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_URL<span class="token2">,</span> $url<span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span>$ssl<span class="token2">)</span> <span class="token2">{</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_SSL_VERIFYPEER<span class="token2">,</span> FALSE<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_SSL_VERIFYHOST<span class="token2">,</span> FALSE<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_POSTFIELDS<span class="token2">,</span> $msg<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_RETURNTRANSFER<span class="token2">,</span> <span class="token3">1</span><span class="token2">)</span><span class="token2">;</span> $data <span class="token">=</span> <span class="token1">curl_exec</span><span class="token2">(</span>$ch<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_close</span><span class="token2">(</span>$ch<span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> $data<span class="token2">;</span> <span class="token2">}</span> ``` ``` ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">get</span><span class="token2">(</span>$url<span class="token2">)</span> <span class="token2">{</span> $ch <span class="token">=</span> <span class="token1">curl_init</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">preg_match</span><span class="token2">(</span><span class="token4">'/https:\/\//'</span><span class="token2">,</span> $url<span class="token2">)</span> <span class="token">?</span> $ssl <span class="token">=</span> TRUE <span class="token2">:</span> $ssl <span class="token">=</span> FALSE<span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_URL<span class="token2">,</span> $url<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_HEADER<span class="token2">,</span> <span class="token3">0</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span>$ssl<span class="token2">)</span> <span class="token2">{</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_SSL_VERIFYPEER<span class="token2">,</span> FALSE<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_SSL_VERIFYHOST<span class="token2">,</span> FALSE<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_RETURNTRANSFER<span class="token2">,</span> <span class="token3">1</span><span class="token2">)</span><span class="token2">;</span> $data <span class="token">=</span> <span class="token1">curl_exec</span><span class="token2">(</span>$ch<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_close</span><span class="token2">(</span>$ch<span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> $data<span class="token2">;</span> <span class="token2">}</span> ``` ``` /\*\* - 导出csv数据,不支持大数据,大数据用分页导出 - $arr = array( - array('用户名','密码','邮箱'), - array( - array('A用户','123456','xiaohai1@zhongsou.com'), - array('B用户','213456','xiaohai2@zhongsou.com'), - array('C用户','123456','xiaohai3@zhongsou.com') - )); - putcsv("导出文件",$arr); - - 导出csv模板 - $arr = array(array('用户名','密码','邮箱')); - putcsv("导出模板",$arr); - 文件名不带.csv,自动加 - $filename 导出文件名 - $arr 导出数组 \*/ ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">putcsv</span><span class="token2">(</span>$filename<span class="token2">,</span> $arr<span class="token2">)</span> <span class="token2">{</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">empty</span><span class="token2">(</span>$arr<span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token3">false</span><span class="token2">;</span> <span class="token2">}</span> $export_str <span class="token">=</span> <span class="token1">implode</span><span class="token2">(</span><span class="token4">','</span><span class="token2">,</span> $arr<span class="token2">[</span><span class="token3">0</span><span class="token2">]</span><span class="token2">)</span> <span class="token2">.</span> <span class="token4">"\n"</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span><span class="token">!</span><span class="token1">empty</span><span class="token2">(</span>$arr<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> foreach <span class="token2">(</span>$arr<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span> as $k <span class="token">=</span><span class="token">></span> $v<span class="token2">)</span> <span class="token2">{</span> $export_str <span class="token2">.</span><span class="token">=</span> <span class="token1">implode</span><span class="token2">(</span><span class="token4">','</span><span class="token2">,</span> $v<span class="token2">)</span> <span class="token2">.</span> <span class="token4">"\n"</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token1">header</span><span class="token2">(</span><span class="token4">"Content-type:application/vnd.ms-excel"</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">header</span><span class="token2">(</span><span class="token4">"Content-Disposition:attachment;filename="</span> <span class="token2">.</span> $filename <span class="token2">.</span> <span class="token1">date</span><span class="token2">(</span><span class="token4">'Y-m-d-H-i-s'</span><span class="token2">)</span> <span class="token2">.</span> <span class="token4">".csv"</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">ob_start</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">ob_end_clean</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> echo <span class="token4">"\xEF\xBB\xBF"</span> <span class="token2">.</span> $export_str<span class="token2">;</span><span class="token6">//解决WPS和excel不乱码</span> <span class="token2">}</span> ``` ``` /\*\* - 导入csv,编码ANSI - read.csv数据 - - 商户名称, 昵称, 手机号 - 惠吃惠喝, 会吃,18291443322 - egeme, 依加米,18923451622 - 徐汇区,上海,18291447788 - 衣服, 买衣服,18291448824 - 米掌柜, MI,18291448822 - $path = 'read.csv'; - $arr= getcsv($path); - 导入csv返回数组,注意导入文件一定要是ANSI编码,也就是WPS和excel打开不乱码 - $path 文件路径 - \*/ ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">getcsv</span><span class="token2">(</span>$path<span class="token2">)</span> <span class="token2">{</span> $handle <span class="token">=</span> <span class="token1">fopen</span><span class="token2">(</span>$path<span class="token2">,</span> <span class="token4">'r'</span><span class="token2">)</span><span class="token2">;</span> $dataArray <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $row <span class="token">=</span> <span class="token3">0</span><span class="token2">;</span> <span class="token5">while</span> <span class="token2">(</span>$data <span class="token">=</span> <span class="token1">fgetcsv</span><span class="token2">(</span>$handle<span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> $num <span class="token">=</span> <span class="token1">count</span><span class="token2">(</span>$data<span class="token2">)</span><span class="token2">;</span> <span class="token5">for</span> <span class="token2">(</span>$i <span class="token">=</span> <span class="token3">0</span><span class="token2">;</span> $i <span class="token"><</span> $num<span class="token2">;</span> $i<span class="token">++</span><span class="token2">)</span> <span class="token2">{</span> $dataArray<span class="token2">[</span>$row<span class="token2">]</span><span class="token2">[</span>$i<span class="token2">]</span> <span class="token">=</span> <span class="token1">mb_convert_encoding</span><span class="token2">(</span>$data<span class="token2">[</span>$i<span class="token2">]</span><span class="token2">,</span> <span class="token4">"utf-8"</span><span class="token2">,</span> <span class="token4">'GBK'</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> $row<span class="token">++</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> $dataArray<span class="token2">;</span> <span class="token2">}</span> ``` ``` ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">isWeixin</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> $agent <span class="token">=</span> $_SERVER <span class="token2">[</span><span class="token4">'HTTP_USER_AGENT'</span><span class="token2">]</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span><span class="token">!</span> strpos <span class="token2">(</span> $agent<span class="token2">,</span> <span class="token4">"icroMessenger"</span> <span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token3">false</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> <span class="token3">true</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">function</span> <span class="token1">hidetel</span><span class="token2">(</span>$phone<span class="token2">)</span><span class="token2">{</span> $IsWhat <span class="token">=</span> <span class="token1">preg_match</span><span class="token2">(</span><span class="token4">'/(0[0-9]{2,3}[-]?[2-9][0-9]{6,7}[-]?[0-9]?)/i'</span><span class="token2">,</span>$phone<span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span>$IsWhat <span class="token">==</span> <span class="token3">1</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">return</span> <span class="token1">preg_replace</span><span class="token2">(</span><span class="token4">'/(0[0-9]{2,3}[-]?[2-9])[0-9]{3,4}([0-9]{3}[-]?[0-9]?)/i'</span><span class="token2">,</span><span class="token4">'$1****$2'</span><span class="token2">,</span>$phone<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> <span class="token5">return</span> <span class="token1">preg_replace</span><span class="token2">(</span><span class="token4">'/(1[34578]{1}[0-9])[0-9]{4}([0-9]{4})/i'</span><span class="token2">,</span><span class="token4">'$1****$2'</span><span class="token2">,</span>$phone<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` /\*\* \* - @param 生成字符长度 $len - @param 生成类型默认大小写数字,0大小写 1数字 2大写 3小写 4中文 $type - @param 添加字符后缀 $addChars - - @return \*/ ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">randstr</span><span class="token2">(</span>$len<span class="token">=</span><span class="token3">6</span><span class="token2">,</span>$type<span class="token">=</span><span class="token4">''</span><span class="token2">,</span>$addChars<span class="token">=</span><span class="token4">''</span><span class="token2">)</span> <span class="token2">{</span> $str <span class="token">=</span><span class="token4">''</span><span class="token2">;</span> <span class="token1">switch</span><span class="token2">(</span>$type<span class="token2">)</span> <span class="token2">{</span> case <span class="token3">0</span><span class="token2">:</span> $chars<span class="token">=</span><span class="token4">'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'</span><span class="token2">.</span>$addChars<span class="token2">;</span> <span class="token5">break</span><span class="token2">;</span> case <span class="token3">1</span><span class="token2">:</span> $chars<span class="token">=</span> <span class="token1">str_repeat</span><span class="token2">(</span><span class="token4">'0123456789'</span><span class="token2">,</span><span class="token3">3</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">break</span><span class="token2">;</span> case <span class="token3">2</span><span class="token2">:</span> $chars<span class="token">=</span><span class="token4">'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</span><span class="token2">.</span>$addChars<span class="token2">;</span> <span class="token5">break</span><span class="token2">;</span> case <span class="token3">3</span><span class="token2">:</span> $chars<span class="token">=</span><span class="token4">'abcdefghijklmnopqrstuvwxyz'</span><span class="token2">.</span>$addChars<span class="token2">;</span> <span class="token5">break</span><span class="token2">;</span> case <span class="token3">4</span><span class="token2">:</span> $chars <span class="token">=</span> <span class="token4">"们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然如应形想制心样干都向变关问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书术状厂须离再目海交权且儿青才证低越际八试规斯近注办布门铁需走议县兵固除般引齿千胜细影济白格效置推空配刀叶率述今选养德话查差半敌始片施响收华觉备名红续均药标记难存测士身紧液派准斤角降维板许破述技消底床田势端感往神便贺村构照容非搞亚磨族火段算适讲按值美态黄易彪服早班麦削信排台声该击素张密害侯草何树肥继右属市严径螺检左页抗苏显苦英快称坏移约巴材省黑武培著河帝仅针怎植京助升王眼她抓含苗副杂普谈围食射源例致酸旧却充足短划剂宣环落首尺波承粉践府鱼随考刻靠够满夫失包住促枝局菌杆周护岩师举曲春元超负砂封换太模贫减阳扬江析亩木言球朝医校古呢稻宋听唯输滑站另卫字鼓刚写刘微略范供阿块某功套友限项余倒卷创律雨让骨远帮初皮播优占死毒圈伟季训控激找叫云互跟裂粮粒母练塞钢顶策双留误础吸阻故寸盾晚丝女散焊功株亲院冷彻弹错散商视艺灭版烈零室轻血倍缺厘泵察绝富城冲喷壤简否柱李望盘磁雄似困巩益洲脱投送奴侧润盖挥距触星松送获兴独官混纪依未突架宽冬章湿偏纹吃执阀矿寨责熟稳夺硬价努翻奇甲预职评读背协损棉侵灰虽矛厚罗泥辟告卵箱掌氧恩爱停曾溶营终纲孟钱待尽俄缩沙退陈讨奋械载胞幼哪剥迫旋征槽倒握担仍呀鲜吧卡粗介钻逐弱脚怕盐末阴丰雾冠丙街莱贝辐肠付吉渗瑞惊顿挤秒悬姆烂森糖圣凹陶词迟蚕亿矩康遵牧遭幅园腔订香肉弟屋敏恢忘编印蜂急拿扩伤飞露核缘游振操央伍域甚迅辉异序免纸夜乡久隶缸夹念兰映沟乙吗儒杀汽磷艰晶插埃燃欢铁补咱芽永瓦倾阵碳演威附牙芽永瓦斜灌欧献顺猪洋腐请透司危括脉宜笑若尾束壮暴企菜穗楚汉愈绿拖牛份染既秋遍锻玉夏疗尖殖井费州访吹荣铜沿替滚客召旱悟刺脑措贯藏敢令隙炉壳硫煤迎铸粘探临薄旬善福纵择礼愿伏残雷延烟句纯渐耕跑泽慢栽鲁赤繁境潮横掉锥希池败船假亮谓托伙哲怀割摆贡呈劲财仪沉炼麻罪祖息车穿货销齐鼠抽画饲龙库守筑房歌寒喜哥洗蚀废纳腹乎录镜妇恶脂庄擦险赞钟摇典柄辩竹谷卖乱虚桥奥伯赶垂途额壁网截野遗静谋弄挂课镇妄盛耐援扎虑键归符庆聚绕摩忙舞遇索顾胶羊湖钉仁音迹碎伸灯避泛亡答勇频皇柳哈揭甘诺概宪浓岛袭谁洪谢炮浇斑讯懂灵蛋闭孩释乳巨徒私银伊景坦累匀霉杜乐勒隔弯绩招绍胡呼痛峰零柴簧午跳居尚丁秦稍追梁折耗碱殊岗挖氏刃剧堆赫荷胸衡勤膜篇登驻案刊秧缓凸役剪川雪链渔啦脸户洛孢勃盟买杨宗焦赛旗滤硅炭股坐蒸凝竟陷枪黎救冒暗洞犯筒您宋弧爆谬涂味津臂障褐陆啊健尊豆拔莫抵桑坡缝警挑污冰柬嘴啥饭塑寄赵喊垫丹渡耳刨虎笔稀昆浪萨茶滴浅拥穴覆伦娘吨浸袖珠雌妈紫戏塔锤震岁貌洁剖牢锋疑霸闪埔猛诉刷狠忽灾闹乔唐漏闻沈熔氯荒茎男凡抢像浆旁玻亦忠唱蒙予纷捕锁尤乘乌智淡允叛畜俘摸锈扫毕璃宝芯爷鉴秘净蒋钙肩腾枯抛轨堂拌爸循诱祝励肯酒绳穷塘燥泡袋朗喂铝软渠颗惯贸粪综墙趋彼届墨碍启逆卸航衣孙龄岭骗休借"</span><span class="token2">.</span>$addChars<span class="token2">;</span> <span class="token5">break</span><span class="token2">;</span> default <span class="token2">:</span> $chars<span class="token">=</span><span class="token4">'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'</span><span class="token2">.</span>$addChars<span class="token2">;</span> <span class="token5">break</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">if</span><span class="token2">(</span>$len<span class="token">></span><span class="token3">10</span> <span class="token2">)</span> <span class="token2">{</span> $chars<span class="token">=</span> $type<span class="token">==</span><span class="token3">1</span><span class="token">?</span> <span class="token1">str_repeat</span><span class="token2">(</span>$chars<span class="token2">,</span>$len<span class="token2">)</span> <span class="token2">:</span> <span class="token1">str_repeat</span><span class="token2">(</span>$chars<span class="token2">,</span><span class="token3">5</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">if</span><span class="token2">(</span>$type<span class="token">!=</span><span class="token3">4</span><span class="token2">)</span> <span class="token2">{</span> $chars <span class="token">=</span> <span class="token1">str_shuffle</span><span class="token2">(</span>$chars<span class="token2">)</span><span class="token2">;</span> $str <span class="token">=</span> <span class="token1">substr</span><span class="token2">(</span>$chars<span class="token2">,</span><span class="token3">0</span><span class="token2">,</span>$len<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> <span class="token5">for</span><span class="token2">(</span>$i<span class="token">=</span><span class="token3">0</span><span class="token2">;</span>$i<span class="token"><</span>$len<span class="token2">;</span>$i<span class="token">++</span><span class="token2">)</span><span class="token2">{</span> $str<span class="token2">.</span><span class="token">=</span> <span class="token1">cutstr</span><span class="token2">(</span>$chars<span class="token2">,</span><span class="token3">1</span><span class="token2">,</span> <span class="token1">floor</span><span class="token2">(</span><span class="token1">mt_rand</span><span class="token2">(</span><span class="token3">0</span><span class="token2">,</span><span class="token1">mb_strlen</span><span class="token2">(</span>$chars<span class="token2">,</span><span class="token4">'utf-8'</span><span class="token2">)</span><span class="token">-</span><span class="token3">1</span><span class="token2">)</span><span class="token2">)</span><span class="token2">,</span><span class="token3">0</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token5">return</span> $str<span class="token2">;</span> <span class="token2">}</span> ``` ``` /\*\* \* - @param 字符串 $str - @param 长度 $length - @param 开始位置 $start - @param 是否显示... $suffix - @param 编码 $charset - - 截取字符串 \*/ ``` <pre class="calibre14">``` <span class="token5">if</span> <span class="token2">(</span><span class="token">!</span><span class="token1">function_exists</span><span class="token2">(</span><span class="token4">'cutstr'</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">function</span> <span class="token1">cutstr</span><span class="token2">(</span>$str<span class="token2">,</span> $length<span class="token2">,</span> $start <span class="token">=</span> <span class="token3">0</span><span class="token2">,</span> $suffix <span class="token">=</span> <span class="token3">true</span><span class="token2">,</span> $charset <span class="token">=</span> <span class="token4">"utf-8"</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">function_exists</span><span class="token2">(</span><span class="token4">"mb_substr"</span><span class="token2">)</span><span class="token2">)</span> $slice <span class="token">=</span> <span class="token1">mb_substr</span><span class="token2">(</span>$str<span class="token2">,</span> $start<span class="token2">,</span> $length<span class="token2">,</span> $charset<span class="token2">)</span><span class="token2">;</span> elseif <span class="token2">(</span><span class="token1">function_exists</span><span class="token2">(</span><span class="token4">'iconv_substr'</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> $slice <span class="token">=</span> <span class="token1">iconv_substr</span><span class="token2">(</span>$str<span class="token2">,</span> $start<span class="token2">,</span> $length<span class="token2">,</span> $charset<span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span><span class="token3">false</span> <span class="token">===</span> $slice<span class="token2">)</span> <span class="token2">{</span> $slice <span class="token">=</span> <span class="token4">''</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> $re<span class="token2">[</span><span class="token4">'utf-8'</span><span class="token2">]</span> <span class="token">=</span> <span class="token4">"/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"</span><span class="token2">;</span> $re<span class="token2">[</span><span class="token4">'gb2312'</span><span class="token2">]</span> <span class="token">=</span> <span class="token4">"/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"</span><span class="token2">;</span> $re<span class="token2">[</span><span class="token4">'gbk'</span><span class="token2">]</span> <span class="token">=</span> <span class="token4">"/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"</span><span class="token2">;</span> $re<span class="token2">[</span><span class="token4">'big5'</span><span class="token2">]</span> <span class="token">=</span> <span class="token4">"/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"</span><span class="token2">;</span> <span class="token1">preg_match_all</span><span class="token2">(</span>$re<span class="token2">[</span>$charset<span class="token2">]</span><span class="token2">,</span> $str<span class="token2">,</span> $match<span class="token2">)</span><span class="token2">;</span> $slice <span class="token">=</span> <span class="token1">join</span><span class="token2">(</span><span class="token4">""</span><span class="token2">,</span> <span class="token1">array_slice</span><span class="token2">(</span>$match<span class="token2">[</span><span class="token3">0</span><span class="token2">]</span><span class="token2">,</span> $start<span class="token2">,</span> $length<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> $suffix <span class="token">?</span> $slice <span class="token2">.</span> <span class="token4">'...'</span> <span class="token2">:</span> $slice<span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` /\*\* \* - @param 字节大小 $size - @param 保留小数位数 $dec - - 格式化文件大小 \*/ ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">getfileSize</span><span class="token2">(</span>$size<span class="token2">,</span> $dec<span class="token">=</span><span class="token3">2</span><span class="token2">)</span> <span class="token2">{</span> $a <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">"B"</span><span class="token2">,</span> <span class="token4">"KB"</span><span class="token2">,</span> <span class="token4">"MB"</span><span class="token2">,</span> <span class="token4">"GB"</span><span class="token2">,</span> <span class="token4">"TB"</span><span class="token2">,</span> <span class="token4">"PB"</span><span class="token2">)</span><span class="token2">;</span> $pos <span class="token">=</span> <span class="token3">0</span><span class="token2">;</span> <span class="token5">while</span> <span class="token2">(</span>$size <span class="token">>=</span> <span class="token3">1024</span><span class="token2">)</span> <span class="token2">{</span> $size <span class="token">/</span><span class="token">=</span> <span class="token3">1024</span><span class="token2">;</span> $pos<span class="token">++</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> <span class="token1">round</span><span class="token2">(</span>$size<span class="token2">,</span>$dec<span class="token2">)</span><span class="token2">.</span><span class="token4">" "</span><span class="token2">.</span>$a<span class="token2">[</span>$pos<span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` 概率算法 ``` <pre class="calibre14">``` proArr <span class="token1">array</span><span class="token2">(</span><span class="token3">100</span><span class="token2">,</span><span class="token3">200</span><span class="token2">,</span><span class="token3">300</span>,<span class="token3">400</span><span class="token2">)</span> <span class="token5">function</span> <span class="token1">get_prize</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span><span class="token6">//获取中奖</span> $prize_arr <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">,</span><span class="token4">'prize'</span><span class="token">=</span><span class="token">></span><span class="token4">'平板电脑'</span><span class="token2">,</span><span class="token4">'v'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">)</span><span class="token2">,</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">2</span><span class="token2">,</span><span class="token4">'prize'</span><span class="token">=</span><span class="token">></span><span class="token4">'数码相机'</span><span class="token2">,</span><span class="token4">'v'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">)</span><span class="token2">,</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">3</span><span class="token2">,</span><span class="token4">'prize'</span><span class="token">=</span><span class="token">></span><span class="token4">'音箱设备'</span><span class="token2">,</span><span class="token4">'v'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">)</span><span class="token2">,</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">4</span><span class="token2">,</span><span class="token4">'prize'</span><span class="token">=</span><span class="token">></span><span class="token4">'4G优盘'</span><span class="token2">,</span><span class="token4">'v'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">)</span><span class="token2">,</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">5</span><span class="token2">,</span><span class="token4">'prize'</span><span class="token">=</span><span class="token">></span><span class="token4">'10Q币'</span><span class="token2">,</span><span class="token4">'v'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">)</span><span class="token2">,</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">6</span><span class="token2">,</span><span class="token4">'prize'</span><span class="token">=</span><span class="token">></span><span class="token4">'下次没准就能中哦'</span><span class="token2">,</span><span class="token4">'v'</span><span class="token">=</span><span class="token">></span><span class="token3">95</span><span class="token2">)</span><span class="token2">,</span> <span class="token2">)</span><span class="token2">;</span> foreach <span class="token2">(</span>$prize_arr as $key <span class="token">=</span><span class="token">></span> $val<span class="token2">)</span> <span class="token2">{</span> $arr<span class="token2">[</span>$val<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span><span class="token2">]</span> <span class="token">=</span> $val<span class="token2">[</span><span class="token4">'v'</span><span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span> $ridk <span class="token">=</span> <span class="token1">getrand</span><span class="token2">(</span>$arr<span class="token2">)</span><span class="token2">;</span> <span class="token6">//根据概率获取奖项id</span> $res<span class="token2">[</span><span class="token4">'yes'</span><span class="token2">]</span> <span class="token">=</span> $prize_arr<span class="token2">[</span>$ridk<span class="token">-</span><span class="token3">1</span><span class="token2">]</span><span class="token2">[</span><span class="token4">'prize'</span><span class="token2">]</span><span class="token2">;</span> <span class="token6">//中奖项</span> <span class="token1">unset</span><span class="token2">(</span>$prize_arr<span class="token2">[</span>$ridk<span class="token">-</span><span class="token3">1</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">//将中奖项从数组中剔除,剩下未中奖项</span> <span class="token1">shuffle</span><span class="token2">(</span>$prize_arr<span class="token2">)</span><span class="token2">;</span> <span class="token6">//打乱数组顺序</span> <span class="token5">for</span><span class="token2">(</span>$i<span class="token">=</span><span class="token3">0</span><span class="token2">;</span>$i<span class="token"><</span><span class="token1">count</span><span class="token2">(</span>$prize_arr<span class="token2">)</span><span class="token2">;</span>$i<span class="token">++</span><span class="token2">)</span><span class="token2">{</span> $pr<span class="token2">[</span><span class="token2">]</span> <span class="token">=</span> $prize_arr<span class="token2">[</span>$i<span class="token2">]</span><span class="token2">[</span><span class="token4">'prize'</span><span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span> $res<span class="token2">[</span><span class="token4">'no'</span><span class="token2">]</span> <span class="token">=</span> $pr<span class="token2">;</span> <span class="token5">return</span> $res<span class="token2">;</span> <span class="token2">}</span> ``` ``` ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">getRand</span><span class="token2">(</span>$proArr<span class="token2">)</span> <span class="token2">{</span> $result <span class="token">=</span> <span class="token4">''</span><span class="token2">;</span> $proSum <span class="token">=</span> <span class="token1">array_sum</span><span class="token2">(</span>$proArr<span class="token2">)</span><span class="token2">;</span> foreach <span class="token2">(</span>$proArr as $key <span class="token">=</span><span class="token">></span> $proCur<span class="token2">)</span> <span class="token2">{</span> $randNum <span class="token">=</span> <span class="token1">mt_rand</span><span class="token2">(</span><span class="token3">1</span><span class="token2">,</span> $proSum<span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span>$randNum <span class="token"><=</span> $proCur<span class="token2">)</span> <span class="token2">{</span> $result <span class="token">=</span> $key<span class="token2">;</span> <span class="token5">break</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> $proSum <span class="token">-</span><span class="token">=</span> $proCur<span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> unset <span class="token2">(</span>$proArr<span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> $result<span class="token2">;</span> <span class="token2">}</span> ``` ``` 去除空行与空格 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">trimStr</span><span class="token2">(</span>$str<span class="token2">)</span> <span class="token2">{</span> $str <span class="token">=</span> <span class="token1">trim</span><span class="token2">(</span>$str<span class="token2">)</span><span class="token2">;</span> $str <span class="token">=</span> <span class="token1">preg_replace</span><span class="token2">(</span><span class="token4">"/\t/"</span><span class="token2">,</span><span class="token4">""</span><span class="token2">,</span>$str<span class="token2">)</span><span class="token2">;</span> $str <span class="token">=</span> <span class="token1">preg_replace</span><span class="token2">(</span><span class="token4">"/\r\n/"</span><span class="token2">,</span><span class="token4">""</span><span class="token2">,</span>$str<span class="token2">)</span><span class="token2">;</span> $str <span class="token">=</span> <span class="token1">preg_replace</span><span class="token2">(</span><span class="token4">"/\r/"</span><span class="token2">,</span><span class="token4">""</span><span class="token2">,</span>$str<span class="token2">)</span><span class="token2">;</span> $str <span class="token">=</span> <span class="token1">preg_replace</span><span class="token2">(</span><span class="token4">"/\n/"</span><span class="token2">,</span><span class="token4">""</span><span class="token2">,</span>$str<span class="token2">)</span><span class="token2">;</span> $str <span class="token">=</span> <span class="token1">preg_replace</span><span class="token2">(</span><span class="token4">"/ /"</span><span class="token2">,</span><span class="token4">""</span><span class="token2">,</span>$str<span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> <span class="token1">trim</span><span class="token2">(</span>$str<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` //去除数组中值两端空格,支持excel ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">trimArray</span><span class="token2">(</span>$Input<span class="token2">)</span><span class="token2">{</span> <span class="token5">if</span> <span class="token2">(</span><span class="token">!</span><span class="token1">is_array</span><span class="token2">(</span>$Input<span class="token2">)</span><span class="token2">)</span> <span class="token5">return</span> <span class="token1">preg_replace</span><span class="token2">(</span><span class="token4">"/(\s|\&nbsp\;| |\xc2\xa0)/"</span><span class="token2">,</span><span class="token4">""</span><span class="token2">,</span>$Input<span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> <span class="token1">array_map</span><span class="token2">(</span><span class="token4">'TrimArray'</span><span class="token2">,</span> $Input<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` ``` <pre class="calibre14">``` <span class="token5">if</span> <span class="token2">(</span><span class="token">!</span><span class="token1">function_exists</span><span class="token2">(</span><span class="token4">'getip'</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">function</span> <span class="token1">getIp</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> $ip <span class="token">=</span> <span class="token1">isset</span><span class="token2">(</span>$_SERVER<span class="token2">[</span><span class="token4">'REMOTE_ADDR'</span><span class="token2">]</span><span class="token2">)</span> <span class="token">?</span> $_SERVER<span class="token2">[</span><span class="token4">'REMOTE_ADDR'</span><span class="token2">]</span> <span class="token2">:</span> <span class="token4">''</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span><span class="token">!</span><span class="token1">preg_match</span><span class="token2">(</span><span class="token4">"/^\d+\.\d+\.\d+\.\d+$/"</span><span class="token2">,</span> $ip<span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> $ip <span class="token">=</span> <span class="token4">'0'</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> $ip<span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> 获取内存 <span class="token5">function</span> <span class="token1">getMemory</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">return</span> <span class="token1">round</span><span class="token2">(</span><span class="token2">(</span><span class="token1">memory_get_usage</span><span class="token2">(</span><span class="token2">)</span><span class="token">/</span><span class="token3">1024</span><span class="token">/</span><span class="token3">1024</span><span class="token2">)</span><span class="token2">,</span><span class="token3">3</span><span class="token2">)</span><span class="token2">.</span><span class="token4">"M"</span><span class="token2">;</span> <span class="token2">}</span> 随即颜色 <span class="token5">function</span> <span class="token1">randColor</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span> $char<span class="token">=</span><span class="token4">'abcdef0123456789'</span><span class="token2">;</span> $str<span class="token">=</span><span class="token4">''</span><span class="token2">;</span> <span class="token5">for</span><span class="token2">(</span>$i<span class="token">=</span><span class="token3">0</span><span class="token2">;</span>$i<span class="token"><</span><span class="token3">6</span><span class="token2">;</span>$i<span class="token">++</span><span class="token2">)</span><span class="token2">{</span> $str <span class="token2">.</span><span class="token">=</span> <span class="token1">substr</span><span class="token2">(</span>$char<span class="token2">,</span><span class="token1">mt_rand</span><span class="token2">(</span><span class="token3">0</span><span class="token2">,</span><span class="token3">15</span><span class="token2">)</span><span class="token2">,</span><span class="token3">1</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> <span class="token4">'#'</span><span class="token2">.</span>$str<span class="token2">;</span> <span class="token2">}</span> ``` ``` xml与数组互换 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">arr2xml</span><span class="token2">(</span>$arr<span class="token2">,</span> $level <span class="token">=</span> <span class="token3">1</span><span class="token2">,</span> $ptagname <span class="token">=</span> <span class="token4">''</span><span class="token2">)</span> <span class="token2">{</span> $s <span class="token">=</span> $level <span class="token">==</span> <span class="token3">1</span> <span class="token">?</span> <span class="token4">"<xml>"</span> <span class="token2">:</span> <span class="token4">''</span><span class="token2">;</span> <span class="token1">foreach</span><span class="token2">(</span>$arr as $tagname <span class="token">=</span><span class="token">></span> $value<span class="token2">)</span> <span class="token2">{</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">is_numeric</span><span class="token2">(</span>$tagname<span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> $tagname <span class="token">=</span> $value<span class="token2">[</span><span class="token4">'TagName'</span><span class="token2">]</span><span class="token2">;</span> <span class="token1">unset</span><span class="token2">(</span>$value<span class="token2">[</span><span class="token4">'TagName'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">if</span><span class="token2">(</span><span class="token">!</span><span class="token1">is_array</span><span class="token2">(</span>$value<span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> $s <span class="token2">.</span><span class="token">=</span> <span class="token4">"<{$tagname}>"</span><span class="token2">.</span><span class="token2">(</span><span class="token">!</span><span class="token1">is_numeric</span><span class="token2">(</span>$value<span class="token2">)</span> <span class="token">?</span> <span class="token4">'<![CDATA['</span> <span class="token2">:</span> <span class="token4">''</span><span class="token2">)</span><span class="token2">.</span>$value<span class="token2">.</span><span class="token2">(</span><span class="token">!</span><span class="token1">is_numeric</span><span class="token2">(</span>$value<span class="token2">)</span> <span class="token">?</span> <span class="token4">']]>'</span> <span class="token2">:</span> <span class="token4">''</span><span class="token2">)</span><span class="token2">.</span><span class="token4">"</{$tagname}>"</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> $s <span class="token2">.</span><span class="token">=</span> <span class="token4">"<{$tagname}>"</span><span class="token2">.</span>self<span class="token2">:</span><span class="token2">:</span><span class="token1">arr2xml</span><span class="token2">(</span>$value<span class="token2">,</span> $level <span class="token">+</span> <span class="token3">1</span><span class="token2">)</span><span class="token2">.</span><span class="token4">"</{$tagname}>"</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> $s <span class="token">=</span> <span class="token1">preg_replace</span><span class="token2">(</span><span class="token4">"/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/"</span><span class="token2">,</span> <span class="token4">' '</span><span class="token2">,</span> $s<span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> $level <span class="token">==</span> <span class="token3">1</span> <span class="token">?</span> $s<span class="token2">.</span><span class="token4">"</xml>"</span> <span class="token2">:</span> $s<span class="token2">;</span> <span class="token2">}</span> <span class="token6">/** xml转换数组 */</span> <span class="token5">function</span> <span class="token1">xml2arr</span><span class="token2">(</span>$xml<span class="token2">)</span> <span class="token2">{</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">empty</span><span class="token2">(</span>$xml<span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token1">array</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> $result <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $xmlobj <span class="token">=</span> <span class="token1">simplexml_load_string</span><span class="token2">(</span>$xml<span class="token2">,</span> <span class="token4">'SimpleXMLElement'</span><span class="token2">,</span> LIBXML_NOCDATA<span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span>$xmlobj <span class="token5">instanceof</span> <span class="token1">SimpleXMLElement</span><span class="token2">)</span> <span class="token2">{</span> $result <span class="token">=</span> <span class="token1">json_decode</span><span class="token2">(</span><span class="token1">json_encode</span><span class="token2">(</span>$xmlobj<span class="token2">)</span><span class="token2">,</span> <span class="token3">true</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">is_array</span><span class="token2">(</span>$result<span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> $result<span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token1">array</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> <span class="token5">return</span> $result<span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` 创建文件或文件夹 ``` <pre class="calibre14">``` <span class="token6">/* 创建文件或文件夹 参数是数组 ["qq/","qq.txt","qqq/tt/"]; */</span> <span class="token5">function</span> <span class="token1">fileCreate</span><span class="token2">(</span>$files<span class="token2">)</span> <span class="token2">{</span> foreach <span class="token2">(</span>$files as $key <span class="token">=</span><span class="token">></span> $value<span class="token2">)</span> <span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span><span class="token1">substr</span><span class="token2">(</span>$value<span class="token2">,</span> <span class="token">-</span><span class="token3">1</span><span class="token2">)</span> <span class="token">==</span> <span class="token4">'/'</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span><span class="token">!</span><span class="token1">is_dir</span><span class="token2">(</span>$value<span class="token2">)</span><span class="token2">)</span><span class="token2">{</span><span class="token1">mkdir</span><span class="token2">(</span>$value<span class="token2">,</span> <span class="token3">0777</span><span class="token2">,</span> <span class="token3">true</span><span class="token2">)</span><span class="token2">;</span><span class="token2">}</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span><span class="token">!</span><span class="token1">file_exists</span><span class="token2">(</span>$value<span class="token2">)</span><span class="token2">)</span><span class="token2">{</span><span class="token1">file_put_contents</span><span class="token2">(</span>$value<span class="token2">,</span> <span class="token4">''</span><span class="token2">)</span><span class="token2">;</span><span class="token2">}</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` /\*\* \* - @param undefined $type 弹出类型 1,2,3 - @param undefined $info 提示语 - @param undefined $url 跳转地址 - - @return \*/ ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">alert</span><span class="token2">(</span>$type<span class="token">=</span><span class="token3">1</span><span class="token2">,</span>$info<span class="token">=</span><span class="token4">""</span><span class="token2">,</span>$url<span class="token">=</span><span class="token4">""</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span><span class="token3">1</span><span class="token">==</span>$type<span class="token2">)</span><span class="token2">{</span><span class="token6">//自动关闭</span> $strs <span class="token">=</span> <span class="token1">empty</span><span class="token2">(</span>$info<span class="token2">)</span><span class="token">?</span><span class="token4">""</span><span class="token2">:</span><span class="token4">"alert('$info');"</span><span class="token2">;</span> echo <span class="token4">"<script>"</span><span class="token2">.</span>$strs<span class="token2">.</span>"document<span class="token2">.</span><span class="token1">addEventListener</span><span class="token2">(</span><span class="token4">'WeixinJSBridgeReady'</span><span class="token2">,</span> <span class="token5">function</span> <span class="token1">onBridgeReady</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> WeixinJSBridge<span class="token2">.</span><span class="token1">call</span><span class="token2">(</span><span class="token4">'closeWindow'</span><span class="token2">)</span><span class="token2">;</span><span class="token2">}</span><span class="token2">)</span><span class="token2">;</span><span class="token"><</span><span class="token">/</span>script<span class="token">></span>"<span class="token2">;</span>exit<span class="token2">;</span> <span class="token2">}</span><span class="token1">elseif</span><span class="token2">(</span><span class="token3">2</span><span class="token">==</span>$type<span class="token2">)</span><span class="token2">{</span><span class="token6">//显示跳转中...</span> $urls <span class="token">=</span> <span class="token1">empty</span><span class="token2">(</span>$url<span class="token2">)</span><span class="token">?</span><span class="token4">""</span><span class="token2">:</span><span class="token4">'location.href="'</span><span class="token2">.</span>$url<span class="token2">.</span><span class="token4">'";'</span><span class="token2">;</span> $strs <span class="token">=</span> <span class="token1">empty</span><span class="token2">(</span>$info<span class="token2">)</span><span class="token">?</span><span class="token4">"正在跳转中..."</span><span class="token2">:</span>$info<span class="token2">;</span> echo <span class="token4">"<meta charset='utf-8'>"</span><span class="token2">;</span> <span class="token1">die</span><span class="token2">(</span><span class="token4">'<script type="text/javascript">document.write("<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,user-scalable=0\"><div style=\"font-size:16px;margin:30px auto;text-align:center;\">'</span><span class="token2">.</span>$strs<span class="token2">.</span><span class="token4">'</div>");'</span><span class="token2">.</span>$urls<span class="token2">.</span><span class="token4">';</script>'</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token1">elseif</span><span class="token2">(</span><span class="token3">3</span><span class="token">==</span>$type<span class="token2">)</span><span class="token2">{</span><span class="token6">//普通弹出,跳转</span> $strs <span class="token">=</span> <span class="token1">empty</span><span class="token2">(</span>$info<span class="token2">)</span><span class="token">?</span><span class="token4">""</span><span class="token2">:</span><span class="token4">"alert('$info');"</span><span class="token2">;</span> $urls <span class="token">=</span> <span class="token1">empty</span><span class="token2">(</span>$url<span class="token2">)</span><span class="token">?</span><span class="token4">""</span><span class="token2">:</span><span class="token4">'location.href="'</span><span class="token2">.</span>$url<span class="token2">.</span><span class="token4">'";'</span><span class="token2">;</span> <span class="token1">die</span><span class="token2">(</span><span class="token4">'<script type="text/javascript">'</span><span class="token2">.</span>$strs<span class="token2">.</span>$urls<span class="token2">.</span><span class="token4">'</script>'</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token1">elseif</span><span class="token2">(</span><span class="token3">4</span><span class="token">==</span>$type<span class="token2">)</span><span class="token2">{</span><span class="token6">//蓝色i</span> echo <span class="token4">"<meta charset='utf-8'>"</span><span class="token2">;</span> <span class="token1">die</span><span class="token2">(</span><span class="token4">'<script>document.write("<title>提示</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=0\"><link rel=\"stylesheet\" href=\"https://res.wx.qq.com/open/libs/weui/0.4.3/weui.min.css\"><div class=\"weui_msg\"><div class=\"weui_icon_area\"><i class=\"weui_icon_info weui_icon_msg\"></i></div><div class=\"weui_text_area\"><h4 class=\"weui_msg_title\">'</span><span class="token2">.</span>$info<span class="token2">.</span><span class="token4">'</h4></div></div>");document.addEventListener("WeixinJSBridgeReady", function onBridgeReady() {WeixinJSBridge.call("hideOptionMenu");});</script>'</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token1">elseif</span><span class="token2">(</span><span class="token3">5</span><span class="token">==</span>$type<span class="token2">)</span><span class="token2">{</span><span class="token6">//红色警告!</span> echo <span class="token4">"<meta charset='utf-8'>"</span><span class="token2">;</span> <span class="token1">die</span><span class="token2">(</span><span class="token4">'<script>document.write("<title>提示</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=0\"><link rel=\"stylesheet\" href=\"https://res.wx.qq.com/open/libs/weui/0.4.3/weui.min.css\"><div class=\"weui_msg\"><div class=\"weui_icon_area\"><i class=\"weui_icon_msg weui_icon_warn\"></i></div><div class=\"weui_text_area\"><h4 class=\"weui_msg_title\">'</span><span class="token2">.</span>$info<span class="token2">.</span><span class="token4">'</h4></div></div>");document.addEventListener("WeixinJSBridgeReady", function onBridgeReady() {WeixinJSBridge.call("hideOptionMenu");});</script>'</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token1">elseif</span><span class="token2">(</span><span class="token3">6</span><span class="token">==</span>$type<span class="token2">)</span><span class="token2">{</span><span class="token6">//绿色成功√</span> echo <span class="token4">"<meta charset='utf-8'>"</span><span class="token2">;</span> <span class="token1">die</span><span class="token2">(</span><span class="token4">'<script>document.write("<title>提示</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=0\"><link rel=\"stylesheet\" href=\"https://res.wx.qq.com/open/libs/weui/0.4.3/weui.min.css\"><div class=\"weui_msg\"><div class=\"weui_icon_area\"><i class=\"weui_icon_msg weui_icon_success\"></i></div><div class=\"weui_text_area\"><h4 class=\"weui_msg_title\">'</span><span class="token2">.</span>$info<span class="token2">.</span><span class="token4">'</h4></div></div>");document.addEventListener("WeixinJSBridgeReady", function onBridgeReady() {WeixinJSBridge.call("hideOptionMenu");});</script>'</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` 数组排序:数组/排序字段/升降序asc升序,desc降序 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">array_sort</span><span class="token2">(</span>$array<span class="token2">,</span> $keys<span class="token2">,</span> $type<span class="token">=</span><span class="token4">'asc'</span><span class="token2">)</span><span class="token2">{</span> $keysvalue <span class="token">=</span> $new_array <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> foreach <span class="token2">(</span>$array as $k <span class="token">=</span><span class="token">></span> $v<span class="token2">)</span><span class="token2">{</span> $keysvalue<span class="token2">[</span>$k<span class="token2">]</span> <span class="token">=</span> $v<span class="token2">[</span>$keys<span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">if</span><span class="token2">(</span>$type <span class="token">==</span> <span class="token4">'asc'</span><span class="token2">)</span><span class="token2">{</span> <span class="token1">asort</span><span class="token2">(</span>$keysvalue<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> <span class="token1">arsort</span><span class="token2">(</span>$keysvalue<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token1">reset</span><span class="token2">(</span>$keysvalue<span class="token2">)</span><span class="token2">;</span> foreach <span class="token2">(</span>$keysvalue as $k <span class="token">=</span><span class="token">></span> $v<span class="token2">)</span><span class="token2">{</span> $new_array<span class="token2">[</span>$k<span class="token2">]</span> <span class="token">=</span> $array<span class="token2">[</span>$k<span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> $new_array<span class="token2">;</span> <span class="token2">}</span> ``` ``` /\*\* - 计算两地之间距离 - @param undefined $lat1 经度1 - @param undefined $lng1 纬度1 - @param undefined $lat2 经度2 - @param undefined $lng2 纬度2 - @param 1 $len\_type 1是米,2,千米 - @param undefined $decimal,保留两位小数 - - @return \*/ ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">getDistance</span><span class="token2">(</span>$lat1<span class="token2">,</span> $lng1<span class="token2">,</span> $lat2<span class="token2">,</span> $lng2<span class="token2">,</span> $len_type <span class="token">=</span> <span class="token3">1</span><span class="token2">,</span> $decimal <span class="token">=</span> <span class="token3">2</span><span class="token2">)</span> <span class="token2">{</span> $pi <span class="token">=</span> <span class="token3">3.1415926000000001</span><span class="token2">;</span> $er <span class="token">=</span> <span class="token3">6378.1369999999997</span><span class="token2">;</span> $radLat1 <span class="token">=</span> <span class="token2">(</span>$lat1 <span class="token">*</span> $pi<span class="token2">)</span> <span class="token">/</span> <span class="token3">180</span><span class="token2">;</span> $radLat2 <span class="token">=</span> <span class="token2">(</span>$lat2 <span class="token">*</span> $pi<span class="token2">)</span> <span class="token">/</span> <span class="token3">180</span><span class="token2">;</span> $a <span class="token">=</span> $radLat1 <span class="token">-</span> $radLat2<span class="token2">;</span> $b <span class="token">=</span> <span class="token2">(</span><span class="token2">(</span>$lng1 <span class="token">*</span> $pi<span class="token2">)</span> <span class="token">/</span> <span class="token3">180</span><span class="token2">)</span> <span class="token">-</span> <span class="token2">(</span><span class="token2">(</span>$lng2 <span class="token">*</span> $pi<span class="token2">)</span> <span class="token">/</span> <span class="token3">180</span><span class="token2">)</span><span class="token2">;</span> $s <span class="token">=</span> <span class="token3">2</span> <span class="token">*</span> <span class="token1">asin</span><span class="token2">(</span><span class="token1">sqrt</span><span class="token2">(</span><span class="token1">pow</span><span class="token2">(</span><span class="token1">sin</span><span class="token2">(</span>$a <span class="token">/</span> <span class="token3">2</span><span class="token2">)</span><span class="token2">,</span> <span class="token3">2</span><span class="token2">)</span> <span class="token">+</span> <span class="token2">(</span><span class="token1">cos</span><span class="token2">(</span>$radLat1<span class="token2">)</span> <span class="token">*</span> <span class="token1">cos</span><span class="token2">(</span>$radLat2<span class="token2">)</span> <span class="token">*</span> <span class="token1">pow</span><span class="token2">(</span><span class="token1">sin</span><span class="token2">(</span>$b <span class="token">/</span> <span class="token3">2</span><span class="token2">)</span><span class="token2">,</span> <span class="token3">2</span><span class="token2">)</span><span class="token2">)</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> $s <span class="token">=</span> $s <span class="token">*</span> $er<span class="token2">;</span> $s <span class="token">=</span> <span class="token1">round</span><span class="token2">(</span>$s <span class="token">*</span> <span class="token3">1000</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span><span class="token3">1</span> <span class="token"><</span> $len_type<span class="token2">)</span> <span class="token2">{</span> $s <span class="token">/</span><span class="token">=</span> <span class="token3">1000</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> <span class="token1">round</span><span class="token2">(</span>$s<span class="token2">,</span> $decimal<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">isutf8</span><span class="token2">(</span>$string<span class="token2">)</span> <span class="token2">{</span> $c <span class="token">=</span> <span class="token3">0</span><span class="token2">;</span> $b <span class="token">=</span> <span class="token3">0</span><span class="token2">;</span> $bits <span class="token">=</span> <span class="token3">0</span><span class="token2">;</span> $len <span class="token">=</span> <span class="token1">strlen</span><span class="token2">(</span>$string<span class="token2">)</span><span class="token2">;</span> <span class="token5">for</span><span class="token2">(</span>$i<span class="token">=</span><span class="token3">0</span><span class="token2">;</span> $i<span class="token"><</span>$len<span class="token2">;</span> $i<span class="token">++</span><span class="token2">)</span> <span class="token2">{</span> $c <span class="token">=</span> <span class="token1">ord</span><span class="token2">(</span>$string<span class="token2">[</span>$i<span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span>$c <span class="token">></span> <span class="token3">128</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span><span class="token2">(</span>$c <span class="token">>=</span> <span class="token3">254</span><span class="token2">)</span><span class="token2">)</span> <span class="token5">return</span> <span class="token3">false</span><span class="token2">;</span> <span class="token1">elseif</span><span class="token2">(</span>$c <span class="token">>=</span> <span class="token3">252</span><span class="token2">)</span> $bits<span class="token">=</span><span class="token3">6</span><span class="token2">;</span> <span class="token1">elseif</span><span class="token2">(</span>$c <span class="token">>=</span> <span class="token3">248</span><span class="token2">)</span> $bits<span class="token">=</span><span class="token3">5</span><span class="token2">;</span> <span class="token1">elseif</span><span class="token2">(</span>$c <span class="token">>=</span> <span class="token3">240</span><span class="token2">)</span> $bits<span class="token">=</span><span class="token3">4</span><span class="token2">;</span> <span class="token1">elseif</span><span class="token2">(</span>$c <span class="token">>=</span> <span class="token3">224</span><span class="token2">)</span> $bits<span class="token">=</span><span class="token3">3</span><span class="token2">;</span> <span class="token1">elseif</span><span class="token2">(</span>$c <span class="token">>=</span> <span class="token3">192</span><span class="token2">)</span> $bits<span class="token">=</span><span class="token3">2</span><span class="token2">;</span> <span class="token5">else</span> <span class="token5">return</span> <span class="token3">false</span><span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span><span class="token2">(</span>$i<span class="token">+</span>$bits<span class="token2">)</span> <span class="token">></span> $len<span class="token2">)</span> <span class="token5">return</span> <span class="token3">false</span><span class="token2">;</span> <span class="token5">while</span><span class="token2">(</span>$bits <span class="token">></span> <span class="token3">1</span><span class="token2">)</span> <span class="token2">{</span> $i<span class="token">++</span><span class="token2">;</span> $b<span class="token">=</span><span class="token1">ord</span><span class="token2">(</span>$string<span class="token2">[</span>$i<span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span>$b <span class="token"><</span> <span class="token3">128</span> <span class="token">||</span> $b <span class="token">></span> <span class="token3">191</span><span class="token2">)</span> <span class="token5">return</span> <span class="token3">false</span><span class="token2">;</span> $bits<span class="token">--</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token5">return</span> <span class="token3">true</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` 判断是否POST ``` <pre class="calibre14">``` <span class="token5">if</span><span class="token2">(</span><span class="token">!</span><span class="token1">function_exists</span><span class="token2">(</span><span class="token4">'is_post'</span><span class="token2">)</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">function</span> <span class="token1">is_post</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token1">isset</span><span class="token2">(</span>$_SERVER<span class="token2">[</span><span class="token4">'REQUEST_METHOD'</span><span class="token2">]</span><span class="token2">)</span> <span class="token">&&</span> <span class="token1">strtoupper</span><span class="token2">(</span>$_SERVER<span class="token2">[</span><span class="token4">'REQUEST_METHOD'</span><span class="token2">]</span><span class="token2">)</span><span class="token">==</span><span class="token4">'POST'</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` 判断是否ajax ``` <pre class="calibre14">``` <span class="token5">if</span><span class="token2">(</span><span class="token">!</span><span class="token1">function_exists</span><span class="token2">(</span><span class="token4">'is_ajax'</span><span class="token2">)</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">function</span> <span class="token1">is_ajax</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token1">isset</span><span class="token2">(</span>$_SERVER<span class="token2">[</span><span class="token4">'HTTP_X_REQUESTED_WITH'</span><span class="token2">]</span><span class="token2">)</span> <span class="token">&&</span> <span class="token1">strtoupper</span><span class="token2">(</span>$_SERVER<span class="token2">[</span><span class="token4">'HTTP_X_REQUESTED_WITH'</span><span class="token2">]</span><span class="token2">)</span><span class="token">==</span><span class="token4">'XMLHTTPREQUEST'</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` 判断是否get ``` <pre class="calibre14">``` <span class="token5">if</span><span class="token2">(</span><span class="token">!</span><span class="token1">function_exists</span><span class="token2">(</span><span class="token4">'is_get'</span><span class="token2">)</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">function</span> <span class="token1">is_get</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token1">isset</span><span class="token2">(</span>$_SERVER<span class="token2">[</span><span class="token4">'REQUEST_METHOD'</span><span class="token2">]</span><span class="token2">)</span> <span class="token">&&</span> <span class="token1">strtoupper</span><span class="token2">(</span>$_SERVER<span class="token2">[</span><span class="token4">'REQUEST_METHOD'</span><span class="token2">]</span><span class="token2">)</span><span class="token">==</span><span class="token4">'GET'</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` 字符串与数组互相转换,转换自动判断是否数组 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">str2arr</span><span class="token2">(</span>$var<span class="token2">,</span>$str<span class="token">=</span><span class="token4">','</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span><span class="token1">is_array</span><span class="token2">(</span>$var<span class="token2">)</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">return</span> <span class="token1">implode</span><span class="token2">(</span>$str<span class="token2">,</span>$var<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> <span class="token5">return</span> <span class="token1">explode</span><span class="token2">(</span>$str<span class="token2">,</span>$var<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` /\*\* \* - @param 代码编号 $code - @param 返回的信息 $message - @param 数据 - - @return \*/ ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">json</span><span class="token2">(</span>$code<span class="token">=</span><span class="token3">200</span><span class="token2">,</span>$message<span class="token">=</span><span class="token4">'请求成功'</span><span class="token2">,</span>$list<span class="token">=</span><span class="token1">array</span><span class="token2">(</span><span class="token2">)</span><span class="token2">,</span>$total<span class="token">=</span><span class="token3">0</span><span class="token2">)</span><span class="token2">{</span> $json <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span> <span class="token4">'code'</span><span class="token">=</span><span class="token">></span>$code<span class="token2">,</span> <span class="token4">'msg'</span><span class="token">=</span><span class="token">></span>$message <span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span><span class="token">!</span><span class="token1">empty</span><span class="token2">(</span>$list<span class="token2">)</span><span class="token2">)</span><span class="token2">{</span> $json<span class="token2">[</span><span class="token4">'list'</span><span class="token2">]</span> <span class="token">=</span> $list<span class="token2">;</span> <span class="token2">}</span> <span class="token5">if</span><span class="token2">(</span><span class="token">!</span><span class="token1">empty</span><span class="token2">(</span>$total<span class="token2">)</span><span class="token2">)</span><span class="token2">{</span> $json<span class="token2">[</span><span class="token4">'total'</span><span class="token2">]</span> <span class="token">=</span> $total<span class="token2">;</span> <span class="token2">}</span> <span class="token1">header</span><span class="token2">(</span><span class="token4">'Content-type: application/json'</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">exit</span><span class="token2">(</span><span class="token1">json_encode1</span><span class="token2">(</span>$json<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` 转换实体 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">htmlencode</span><span class="token2">(</span>$var<span class="token2">)</span> <span class="token2">{</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">is_array</span><span class="token2">(</span>$var<span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> foreach <span class="token2">(</span>$var as $key <span class="token">=</span><span class="token">></span> $value<span class="token2">)</span> <span class="token2">{</span> $var<span class="token2">[</span><span class="token1">htmlspecialchars</span><span class="token2">(</span>$key<span class="token2">)</span><span class="token2">]</span> <span class="token">=</span> <span class="token1">html2</span><span class="token2">(</span>$value<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> $var <span class="token">=</span> <span class="token1">str_replace</span><span class="token2">(</span><span class="token4">'&amp;'</span><span class="token2">,</span> <span class="token4">'&'</span><span class="token2">,</span> <span class="token1">htmlspecialchars</span><span class="token2">(</span>$var<span class="token2">,</span> ENT_QUOTES<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> $var<span class="token2">;</span> <span class="token2">}</span> <span class="token5">function</span> <span class="token1">htmldecode</span><span class="token2">(</span>$var<span class="token2">)</span><span class="token2">{</span> <span class="token5">return</span> <span class="token1">htmlspecialchars_decode</span><span class="token2">(</span>$var<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` 生成唯一id字符串 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">createid</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">return</span> <span class="token1">md5</span><span class="token2">(</span><span class="token1">uniqid</span><span class="token2">(</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` 图像信息 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">getimginfo</span><span class="token2">(</span>$src<span class="token2">)</span><span class="token2">{</span> $ext <span class="token">=</span> <span class="token1">fileext</span><span class="token2">(</span>$src<span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span>$ext<span class="token">!=</span><span class="token4">'gif'</span> <span class="token">&&</span> $ext<span class="token">!=</span><span class="token4">'png'</span> <span class="token">&&</span> $ext<span class="token">!=</span><span class="token4">'jpg'</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">return</span> <span class="token4">''</span><span class="token2">;</span> <span class="token2">}</span> $srcarr <span class="token">=</span> <span class="token1">getimagesize</span><span class="token2">(</span>$src<span class="token2">)</span><span class="token2">;</span> $arr <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token3">1</span><span class="token">=</span><span class="token">></span><span class="token4">'gif'</span><span class="token2">,</span><span class="token4">'jpg'</span><span class="token2">,</span><span class="token4">'png'</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> <span class="token4">"像素: "</span><span class="token2">.</span>$srcarr<span class="token2">[</span><span class="token3">0</span><span class="token2">]</span><span class="token2">.</span><span class="token4">" X "</span><span class="token2">.</span>$srcarr<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span><span class="token2">.</span><span class="token4">" 格式: "</span><span class="token2">.</span>$arr<span class="token2">[</span>$srcarr<span class="token2">[</span><span class="token3">2</span><span class="token2">]</span><span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` ``` <pre class="calibre16">``` <span class="token6">/** * 把秒数转换为时分秒的格式 * @param Int $times 时间,单位 秒 * @return String */</span> <span class="token5">function</span> <span class="token1">sec2time</span><span class="token2">(</span>$seconds<span class="token2">)</span><span class="token2">{</span> $seconds <span class="token">=</span> <span class="token2">(</span>int<span class="token2">)</span>$seconds<span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span>$seconds <span class="token">></span><span class="token3">3600</span><span class="token2">)</span><span class="token2">{</span> $hours <span class="token">=</span><span class="token1">intval</span><span class="token2">(</span>$seconds<span class="token">/</span><span class="token3">3600</span><span class="token2">)</span><span class="token2">;</span> $minutes <span class="token">=</span> $seconds <span class="token">%</span> <span class="token3">3600</span><span class="token2">;</span> $time <span class="token">=</span> $hours<span class="token2">.</span><span class="token4">":"</span><span class="token2">.</span><span class="token1">gmstrftime</span><span class="token2">(</span><span class="token4">'%M:%S'</span><span class="token2">,</span>$minutes<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> $time <span class="token">=</span> <span class="token1">gmstrftime</span><span class="token2">(</span><span class="token4">'%H:%M:%S'</span><span class="token2">,</span>$seconds<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">return</span> $time<span class="token2">;</span> <span class="token2">}</span> ``` ```