企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 函数集目录 [TOC=2,3] * * * * * * * * * * * * * * * - ## 浏览器友好的变量输出 * * * * * >[info]#### 1.浏览器友好的变量输出【dump】 ~~~ /** * 浏览器友好的变量输出 * @param mixed $var 变量 * @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串 * @param string $label 标签 默认为空 * @param boolean $strict 是否严谨(严谨输出类型) 默认为FALSE * @return void|string */ function dump($var, $echo = TRUE, $label = null, $strict = FALSE) { $label = ($label === null) ? '' : rtrim($label) . ':'; if (PHP_SAPI == 'cli') { $output = PHP_EOL . $label . $output . PHP_EOL; } else { if (! $strict) { if (ini_get('html_errors')) { $output = print_r($var, true); $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>'; } else { $output = $label . print_r($var, true); } } else { ob_start(); var_dump($var); $output = ob_get_clean(); if (! extension_loaded('xdebug')) { $output = preg_replace('/\]\=\>\n(\s )/m', '] => ', $output); $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>'; } } } if ($echo) { echo ($output); return null; } else return $output; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $var=array( 'a'=>'asd', 'b'=>'b', 'c'=>'c', 'd'=>'d', 'e'=>'e', ); dump($var); ~~~ - #### 函数结果 ~~~ Array ( [a] => asd [b] => b [c] => c [d] => d [e] => e ) ~~~ * * * * * * * * * * * * * * * - ## 返回字节数的大小 * * * * * >[info]#### 1.返回字节数的大小【format_bytes、getSize】 ~~~ /** * 格式化字节大小 * @param number $size 字节数 * @param string $delimiter 数字和单位分隔符 * @return string 格式化后的带单位的大小 */ function format_bytes($size, $delimiter = '') { $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); for ($i = 0; $size >= 1024 && $i < 6; $i++) $size /= 1024; return round($size, 2) . $delimiter . $units[$i]; } /** * 返回字节数的大小 * @param unknown $byte字节数 * @return string */ function getSize($byte){ $b = array ( 1024 * 1024 * 1024 => 'GB', 1024 * 1024 => 'MB', 1024 => 'KB', 1 => 'B' ); foreach ($b as $b_num => $b_v) { $d = $byte / intval($b_num); if ($d >= 1) { $r = round($d); return $r . $b_v; } }; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ echo getSize(1024 * 1024 * 1024 * 1024); ~~~ * * * * * * * * * * * * * * * - ## 获取用户真实 IP * * * * * >[info]#### 1. 获取用户真实 IP【get_ip】 ~~~ /** * 获取用户真实 IP * * @return Ambigous <unknown, string> */ function get_ip() { static $realip; if (isset($_SERVER)) { if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { $realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else if (isset($_SERVER["HTTP_CLIENT_IP"])) { $realip = $_SERVER["HTTP_CLIENT_IP"]; } else { $realip = $_SERVER["REMOTE_ADDR"]; } } else { if (getenv("HTTP_X_FORWARDED_FOR")) { $realip = getenv("HTTP_X_FORWARDED_FOR"); } else if (getenv("HTTP_CLIENT_IP")) { $realip = getenv("HTTP_CLIENT_IP"); } else { $realip = getenv("REMOTE_ADDR"); } } return $realip; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ echo get_ip(); ~~~ * * * * * * * * * * * * * * * - ## 对提交的编辑内容进行处理 * * * * * >[info]#### 1. 对提交的编辑内容进行处理【post_check】 ~~~ /** * 函数名称:post_check() * 函数作用:对提交的编辑内容进行处理 * 参  数:$post: 要提交的内容 * 返 回 值:$post: 返回过滤后的内容 */ function post_check($post) { if (! get_magic_quotes_gpc()) { // 判断magic_quotes_gpc是否为打开 $post = addslashes($post); // 进行magic_quotes_gpc没有打开的情况对提交数据的过滤 } $post = str_replace("_", "\_", $post); // 把 '_'过滤掉 $post = str_replace("%", "\%", $post); // 把 '%'过滤掉 $post = nl2br($post); // 回车转换 $post = htmlspecialchars($post); // html标记转换 return $post; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ echo post_check($post); ~~~ * * * * * * * * * * * * * * * - ## 反转字符串包括中文 * * * * * >[info]#### 1. 反转字符串包括中文【strrev_utf8】 ~~~ /** * 反转字符串包括中文,使用正则和数组反转(strrve()不能使中文反转) * @param unknown $str */ function strrev_utf8($str){ return join("", array_reverse(preg_split("//u",$str))); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $str="hello,你好!"; echo "<br/>"; echo strrev_utf8($str); ~~~ * * * * * * * * * * * * * * * - ## 反转字符串包括中文 * * * * * >[info]#### 1. 反转字符串包括中文【strrev_utf8】 ~~~ /** * 反转字符串包括中文,使用正则和数组反转(strrve()不能使中文反转) * @param unknown $str */ function strrev_utf8($str){ return join("", array_reverse(preg_split("//u",$str))); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $str="hello,你好!"; echo "<br/>"; echo strrev_utf8($str); ~~~ * * * * * * * * * * * * * * * - ## 两个数交换 * * * * * >[info]#### 1. 两个数交换【swap】 ~~~ / * 两个数交换 * @param unknown $a * @param unknown $b * @return string */ function swap($a,$b){ list($b,$a)=array($a,$b); return $a.$b; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $a="y";$b="j"; echo swap($a, $b); ~~~ * * * * * * * * * * * * * * * - ## 返回需要的起始时间 * * * * * >[info]#### 1. 函数【get_time】 ~~~ /** * * @param unknown $time [today/yesterday/this_week/last_week/this_month/last_month/this_quarter/last_quarter/year] * @return multitype:string |string 返回需要的起始时间 */ function get_time($time='today') { switch ($time) { // 今日起始时间 case 'today': { $begin_today = date("Y-m-d H:i:s", mktime(0,0,0,date('m'),date('d'),date('Y'))); $end_today = date("Y-m-d H:i:s", mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1); $data = [ 'begin_time' => $begin_today, 'end_time' => $end_today ]; return $data; break; } // 昨日起始时间 case 'yesterday': { $begin_yesterday = date("Y-m-d H:i:s", mktime(0,0,0,date('m'),date('d')-1,date('Y'))); $end_yesterday = date("Y-m-d H:i:s", mktime(0,0,0,date('m'),date('d'),date('Y'))-1); $data = [ 'begin_time' => $begin_yesterday, 'end_time' => $end_yesterday ]; return $data; break; } // 本周起始时间 case 'this_week': { $begin_this_week = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"))); $end_this_week = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"))); $data = [ 'begin_time' => $begin_this_week, 'end_time' => $end_this_week ]; return $data; break; } // 上周起始时间 case 'last_week': { $begin_last_week = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y"))); $end_last_week = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y"))); $data = [ 'begin_time' => $begin_last_week, 'end_time' => $end_last_week ]; return $data; break; } // 本月起始时间 case 'this_month': { $begin_this_month = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), 1, date("Y"))); $end_this_month = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("t"), date("Y"))); $data = [ 'begin_time' => $begin_this_month, 'end_time' => $end_this_month ]; return $data; break; } // 上月起始时间 case 'last_month': { $begin_last_month = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m") - 1, 1, date("Y"))); $end_last_month = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y"))); $data = [ 'begin_time' => $begin_last_month, 'end_time' => $end_last_month ]; return $data; break; } // 本季度始时间 case 'this_quarter': { $season = ceil((date('n'))/3);//当月是第几季度 $begin_this_quarter = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y'))); $end_this_quarter = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y'))); $data = [ 'begin_time' => $begin_this_quarter, 'end_time' => $end_this_quarter ]; return $data; break; } // 上季度始时间 case 'last_quarter': { $season = ceil((date('n'))/3)-1;//上季度是第几季度 $begin_last_quarter = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y'))); $end_last_quarter = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y'))); $data = [ 'begin_time' => $begin_last_quarter, 'end_time' => $end_last_quarter ]; return $data; break; } // 本季度始时间 case 'this_year': { $begin_this_year = date("Y-m-d H:i:s",mktime(0, 0 , 0,1,1,date("Y",time()))); $end_this_year = date("Y-m-d H:i:s",mktime(23,59,59,12,31,date("Y",time()))); $data = [ 'begin_time' => $begin_this_year, 'end_time' => $end_this_year ]; return $data; break; } default:{ return $data = [ 'begin_time' => '0000-00-00 00:00:00', 'end_time' => date("Y-m-d H:i:s", mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1) ]; return $data; } } } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $data=get_time('last_month'); var_dump($data); ~~~ - #### 函数结果 ~~~ array(2) { ["begin_time"] => string(19) "2016-11-01 00:00:00" ["end_time"] => string(19) "2016-11-30 23:59:59" } ~~~ * * * * * * * * * * * * * * * - ## 返回两个时间的时间差 * * * * * >[info]#### 1. 函数【time_diff】 ~~~ /** * 返回两个时间的时间差(开始时间与结束时间可以换位子,非时间戳) * @param unknown $begin_time 开始时间(非时间戳) * @param unknown $end_time 结束时间(非时间戳) * @return string */ function time_diff( $begin_time, $end_time ){ $begin_time=strtotime($begin_time); $end_time=strtotime($end_time); if ( $begin_time < $end_time ) { $starttime = $begin_time; $endtime = $end_time; } else { $starttime = $end_time; $endtime = $begin_time; } $time = $endtime - $starttime; $year = floor($time / 60 / 60 / 24 / 365); $time -= $year * 60 * 60 * 24 * 365; $month = floor($time / 60 / 60 / 24 / 30); $time -= $month * 60 * 60 * 24 * 30; $week = floor($time / 60 / 60 / 24 / 7); $time -= $week * 60 * 60 * 24 * 7; $day = floor($time / 60 / 60 / 24); $time -= $day * 60 * 60 * 24; $hour = floor($time / 60 / 60); $time -= $hour * 60 * 60; $minute = floor($time / 60); $time -= $minute * 60; $second = $time; $elapse = ''; $unitArr = array( '年' =>'year', '个月'=>'month', '周'=>'week', '天'=>'day', '小时'=>'hour', '分钟'=>'minute', '秒'=>'second' ); foreach ( $unitArr as $k => $v ) { if ( $$v > 0 ) { $elapse .= $$v . $k; /* break; */ //返回粗略时间信息 } } return $elapse; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ echo '发表于' . time_diff('2015-12-21','now') . '前'; ~~~ - #### 函数结果 ~~~ 发表于1年2天9小时56分钟37秒前 ~~~ * * * * * - ## 下载文件函数 * * * * * >[info]#### 1. 函数【download_file】 ~~~ /** * 下载各种文件【不管是否开启php_fileinfo.dll】 * @param unknown $filePath 文件绝对路径(识别中文路径) * @param string $fileName 自定义文件名字,为空时原文件名 * @param string $fileType 自定义文件文件类型,为空时原文件类型 */ function download_file($filePath, $fileName = '', $fileType = '') { if (function_exists('mime_content_type')) { limit_download($filePath, $fileName, $fileType); } else { force_download($filePath, $fileName, $fileType); } } /** * 下载各种文件,成功返回文件字节数,失败返回false【开启php_fileinfo.dll】 * * @param unknown $filePath * 文件路径(识别中文路径) * @param string $fileName * 自定义文件名字,为空时原文件名 * @param string $fileType * 自定义文件文件类型,为空时原文件类型 */ function limit_download($filePath, $fileName = '', $fileType = '') { @set_time_limit(0); if ($filePath == '') { return false; } // 用以解决中文不能显示出来的问题 $filePath = iconv("utf-8", "gb2312", $filePath); $fileName = $fileName ? $fileName : pathinfo($filePath, PATHINFO_FILENAME); $fileType = $fileType ? $fileType : pathinfo($filePath, PATHINFO_EXTENSION); $showName = $fileName . '.' . $fileType; if (is_file($filePath)) { $length = filesize($filePath); $type = mime_content_type($filePath); header("Content-Description: File Transfer"); header('Content-type: ' . $type); header('Content-Length:' . $length); if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { // for IE header('Content-Disposition: attachment; filename="' . rawurlencode($showName) . '"'); } else { header('Content-Disposition: attachment; filename="' . $showName . '"'); } if (@readfile($filePath)==$length){ return $length; }else { return false; } } else { return false; } } /** * 下载各种文件,成功返回文件字节数,失败返回false【不需要开启php_fileinfo.dll】 * * @param unknown $filePath * 文件路径(识别中文路径) * @param string $fileName * 自定义文件名字,为空时原文件名 * @param string $fileType * 自定义文件文件类型,为空时原文件类型 * @return boolean */ function force_download($filePath, $fileName = '', $fileType = '') { @set_time_limit(0); if ($filePath == '') { return FALSE; } // 用以解决中文不能显示出来的问题 $filePath = iconv("utf-8", "gb2312", $filePath); $fileName = $fileName ? $fileName : pathinfo($filePath, PATHINFO_FILENAME); $fileType = $fileType ? $fileType : pathinfo($filePath, PATHINFO_EXTENSION); $showName = $fileName . '.' . $fileType; $extension = $fileType; $mimes = get_mimes(); // Set a default mime if we can't find it if (! isset($mimes[$extension])) { $mime = 'application/octet-stream'; } else { $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension]; } if (is_file($filePath)) { $length = filesize($filePath); // Generate the server headers if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE) { header('Content-Type: "' . $mime . '"'); header('Content-Disposition: attachment; filename="' . $showName . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header("Content-Transfer-Encoding: binary"); header('Pragma: public'); header("Content-Length: " . $length); } else { header('Content-Type: "' . $mime . '"'); header('Content-Disposition: attachment; filename="' . $showName . '"'); header("Content-Transfer-Encoding: binary"); header('Expires: 0'); header('Pragma: no-cache'); header("Content-Length: " . $length); } if (@readfile($filePath)==$length){ return $length; }else { return false; } } else { return false; } } /** * 获取文件的类型 * * @return multitype:string multitype:string */ function get_mimes() { return $mimes = array( 'hqx' => 'application/mac-binhex40', 'cpt' => 'application/mac-compactpro', 'csv' => array( 'text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel' ), 'bin' => 'application/macbinary', 'dms' => 'application/octet-stream', 'lha' => 'application/octet-stream', 'lzh' => 'application/octet-stream', 'exe' => array( 'application/octet-stream', 'application/x-msdownload' ), 'class' => 'application/octet-stream', 'psd' => 'application/x-photoshop', 'so' => 'application/octet-stream', 'sea' => 'application/octet-stream', 'dll' => 'application/octet-stream', 'oda' => 'application/oda', 'pdf' => array( 'application/pdf', 'application/x-download' ), 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'ps' => 'application/postscript', 'smi' => 'application/smil', 'smil' => 'application/smil', 'mif' => 'application/vnd.mif', 'xls' => array( 'application/excel', 'application/vnd.ms-excel', 'application/msexcel' ), 'ppt' => array( 'application/powerpoint', 'application/vnd.ms-powerpoint' ), 'wbxml' => 'application/wbxml', 'wmlc' => 'application/wmlc', 'dcr' => 'application/x-director', 'dir' => 'application/x-director', 'dxr' => 'application/x-director', 'dvi' => 'application/x-dvi', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip', 'php' => 'application/x-httpd-php', 'php4' => 'application/x-httpd-php', 'php3' => 'application/x-httpd-php', 'phtml' => 'application/x-httpd-php', 'phps' => 'application/x-httpd-php-source', 'js' => 'application/x-javascript', 'swf' => 'application/x-shockwave-flash', 'sit' => 'application/x-stuffit', 'tar' => 'application/x-tar', 'tgz' => array( 'application/x-tar', 'application/x-gzip-compressed' ), 'xhtml' => 'application/xhtml+xml', 'xht' => 'application/xhtml+xml', 'zip' => array( 'application/x-zip', 'application/zip', 'application/x-zip-compressed' ), 'mid' => 'audio/midi', 'midi' => 'audio/midi', 'mpga' => 'audio/mpeg', 'mp2' => 'audio/mpeg', 'mp3' => array( 'audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3' ), 'aif' => 'audio/x-aiff', 'aiff' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff', 'ram' => 'audio/x-pn-realaudio', 'rm' => 'audio/x-pn-realaudio', 'rpm' => 'audio/x-pn-realaudio-plugin', 'ra' => 'audio/x-realaudio', 'rv' => 'video/vnd.rn-realvideo', 'wav' => array( 'audio/x-wav', 'audio/wave', 'audio/wav' ), 'bmp' => array( 'image/bmp', 'image/x-windows-bmp' ), 'gif' => 'image/gif', 'jpeg' => array( 'image/jpeg', 'image/pjpeg' ), 'jpg' => array( 'image/jpeg', 'image/pjpeg' ), 'jpe' => array( 'image/jpeg', 'image/pjpeg' ), 'png' => array( 'image/png', 'image/x-png' ), 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'css' => 'text/css', 'html' => 'text/html', 'htm' => 'text/html', 'shtml' => 'text/html', 'txt' => 'text/plain', 'text' => 'text/plain', 'log' => array( 'text/plain', 'text/x-log' ), 'rtx' => 'text/richtext', 'rtf' => 'text/rtf', 'xml' => 'text/xml', 'xsl' => 'text/xml', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mpe' => 'video/mpeg', 'qt' => 'video/quicktime', 'mov' => 'video/quicktime', 'avi' => 'video/x-msvideo', 'movie' => 'video/x-sgi-movie', 'doc' => 'application/msword', 'docx' => array( 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip' ), 'xlsx' => array( 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip' ), 'word' => array( 'application/msword', 'application/octet-stream' ), 'xl' => 'application/excel', 'eml' => 'message/rfc822', 'json' => array( 'application/json', 'text/json' ) ); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ download_file("E:\视频\20P.EP01.mp4",'11','jpg'); ~~~ - #### 函数结果 ~~~ 成功下载文件 ~~~ * * * * * * * * * * * * * * * - ## 批量下载文件到压缩包zip * * * * * >[info]#### 1. 批量下载文件到压缩包zip【downLoadZip】 ~~~ /** * 批量下载文件到压缩包zip * @param $fileUrlArr 文件url地址数组 * @return string */ function downLoadZip($fileUrlArr,$fileName='附件') { set_time_limit(0); ini_set('memory_limit','1024M'); $items = $names = array(); if (! $fileUrlArr) return false; // 用于前端跳转zip链接拼接 $path_redirect = '/zip/' . date('Ymd'); // 临时文件存储地址 $path = '/tmp' . $path_redirect; if (! is_dir($path)) { mkdir($path, 0777, true); } foreach ($fileUrlArr as $key => $value) { $fileContent = ''; $fileContent = curlDownload($value['url']); if ($fileContent) { $__tmp = saveFile($value['url'], $path, $fileContent); $items[] = $__tmp['path']; $names[] = $value['name']?$value['name'].'.' . $__tmp['extension']: $__tmp['filename'].'_' . ($key + 1) . '.' . $__tmp['extension']; } } if ($items) { $zip = new ZipArchive(); $filename = time() . 'download.zip'; $zipname = $path . '/' . $filename; if (! file_exists($zipname)) { $res = $zip->open($zipname, ZipArchive::CREATE | ZipArchive::OVERWRITE); if ($res) { foreach ($items as $k => $v) { $value = explode("/", $v); $end = end($value); $zip->addFile($v, $end); $zip->renameName($end, $names[$k]); } $zip->close(); } else { return ''; } //通过前端js跳转zip地址下载,让不使用php代码下载zip文件 /* if (file_exists($zipname)) { //拼接附件地址 $redirect='http://'.$_SERVER['HTTP_HOST'].$path_redirect.'/'.$filename; header("Location:".$redirect); } */ // 直接写文件的方式下载到客户端 if (file_exists($zipname)) { $fileName=$fileName?:'附件'; header("Cache-Control: public"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename='.$fileName.'.zip'); // 文件名 header("Content-Type: application/zip"); // zip格式的 header("Content-Transfer-Encoding: binary"); // 告诉浏览器,这是二进制文件 header('Content-Length: ' . filesize($zipname)); // 告诉浏览器,文件大小 @readfile($zipname); } // 删除临时文件 foreach ($items as $k => $v){ @unlink($v); } @unlink($zipname); } } return ''; } /** * curl获取链接文件内容 * @param $url * @return mixed|string */ function curlDownload($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $errno = curl_errno($ch); $error = curl_error($ch); $res = curl_exec($ch); curl_close($ch); if ($errno > 0) { return ''; } return $res; } /** * 保存临时文件 * * @param $url * @param $dir * @param $content * @return array */ function saveFile($url, $dir, $content) { $fname = basename($url); // 返回路径中的文件名部分 $str_name = pathinfo($fname); // 以数组的形式返回文件路径的信息 $extension = strtolower($str_name['extension']); // 把扩展名转换成小写 $path = $dir . '/' . md5($url).'.'.$extension;//防止重复 $fp = fopen($path, 'w+'); fwrite($fp, $content); fclose($fp); return array('path'=>$path,'filename'=>$str_name['filename'],'extension'=>$extension); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ // 使用: $fileUrlArr = [ [ 'url' => 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=252599151,170241846&fm=27&gp=0.jpg', 'name' => '' ], [ 'url' => 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506270664766&di=076e206e1f609a33a916947929639eb6&imgtype=0&src=http%3A%2F%2Fstatic.jstv.com%2Fimg%2F2016%2F8%2F11%2F20168111470897319398_0.jpg', 'name' => '名字1' ], [ 'url' => 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506266684565&di=16a400b2326bddafdb1112c87cbd473e&imgtype=0&src=http%3A%2F%2Fwenwen.soso.com%2Fp%2F20110320%2F20110320110539-2023486456.jpg', 'name' => '名字2' ] ]; downLoadZip($fileUrlArr,'压缩包'); ~~~ - #### 函数结果 ~~~ 下载压缩包 ~~~ * * * * * * * * * * * * * * * - ## 生成Token算法 * * * * * >[info]#### 1. 函数【create_token】 ~~~ /** * 生成token算法包括验证token * @param unknown $user_id 用户id * @param string $time 生成token时间 * @param string $unique 唯一的值 * * @param string $type token是否公开【可以为:public, private】 * @return string */ function create_token($user_id='',$time='',$unique='',$type='public'){ $time=$time?$time:time(); $user_id=$user_id?$user_id:''; $unique=$unique?$unique:''; if ($type=='private'){//私有token验证用户id $str='foheart'.$time.$user_id.$unique; }else { $str='foheart'.$time.$unique; } return md5(md5($str)); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ echo create_token($user_id, $time, $id, $type='private'); ~~~ - #### 函数结果 ~~~ 401e4c1fdfb701514fb4560b686bb394 ~~~ * * * * * * * * * * * * * * * - ## 全能字节单位转换 * * * * * >[info]#### 1. 全能字节单位转换【file_size_conv】 ~~~ /** * 全能字节单位转换 * @param unknown $size 需要转换的大小 * @param string $origin_unit 源单位 * @param string $target_unit 目标单位 * @param number $decimals 保留小数 * @return string */ function file_size_conv($size, $origin_unit = 'B', $target_unit = 'auto',$decimals = 0) { $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'); $the_unit = array_search(strtoupper($origin_unit), $units); //初始单位是哪个 //判断是否自动计算 if ($target_unit != 'auto'){ $target_unit = array_search(strtoupper($target_unit), $units); }else { $target_unit=100; } if ($the_unit<=$target_unit){ //循环计算,小单位化大单位 while ($size >= 1024) { $size/=1024; $the_unit++; if ($the_unit == $target_unit)//已符合给定则退出循环吧! break; } }else {//循环计算,大单位化小单位 $levels=$the_unit-$target_unit; if ($levels>=0){ $size=$size*pow(1024,$levels); } $the_unit=$target_unit; } return sprintf("%1\$.{$decimals}f", $size) . $units[$the_unit]; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ echo file_size_conv(2414234234);//自动转换 小化大 echo file_size_conv(2414234234,'KB','GB',3);//自动转换 小化大 echo file_size_conv(2414234234,'GB','MB',0);//大化小 ~~~ - #### 函数结果 ~~~ 2GB 2302.393GB 2472175855616MB ~~~ * * * * * * * * * * * * * * * - ## 返回文件的新名字(处理相同名字的文件) * * * * * >[info]#### 1. 返回文件的新名字【file_newname】 ~~~ /** * 返回文件的新名字(后面添加递增数字显示) * @param unknown $path 文件路径 * @param unknown $filename 文件名 * @return multitype:string Ambigous <string, unknown> */ function file_newname($path, $filename) { if ($pos = strrpos($filename, '.')) { $name = substr($filename, 0, $pos); $ext = substr($filename, $pos); } else { $name = $filename; $ext=''; } $newpath = str_replace('\\', '/', $path) . '/' . $filename; $newname = $name; $counter = 0; while (file_exists($newpath)) { $newname = $name . '(' . $counter . ')'; $newpath = str_replace('\\', '/', $path) . '/' . $newname . $ext; $counter ++; } return $result = [ 'name' => $newname, 'ext' => $ext, 'newname' => $newname . $ext, 'newpath' => $newpath ]; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ echo '<pre>'; print_r(file_newname($path = 'D:\p\ccc/test', $filename = 'wechat.php')); echo '</pre>'; ~~~ - #### 函数结果 ~~~ Array ( [name] => wechat(3) [ext] => .php [newname] => wechat(3).php [newpath] => D:/p/ccc/test/wechat(3).php ) ~~~ * * * * * * * * * * * * * * * - ## 函数以数组或字符串的形式返回关于文件路径的信息 * * * * * >[info]#### 1. 函数以数组或字符串的形式返回关于文件路径的信息【path_info】 ~~~ /** * 函数以数组或字符串的形式返回关于文件路径的信息 * @param unknown $filepath 路径 * @return multitype:string NULL */ function path_info($filepath) { $path = array(); $path['dirname'] = rtrim(substr($filepath, 0, strrpos($filepath, '/')),"/")."/"; $path['basename'] = ltrim(substr($filepath, strrpos($filepath, '/')),"/"); $path['extension'] = substr(strrchr($filepath, '.'), 1); $path['filename'] = ltrim(substr($path['basename'], 0, strrpos($path['basename'], '.')),"/"); return $path; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $file_read = "演示dsad视频sdas.mp4"; echo "<pre>"; print_r(path_info($file_read)); echo "</pre>"; ~~~ - #### 函数结果 ~~~ 演示dsad视频sdas.mp4 Array ( [dirname] => / [basename] => 演示dsad视频sdas.mp4 [extension] => mp4 [filename] => 演示dsad视频sdas ) ~~~ * * * * * * * * * * * * * * * - ## 判断访问是不是手机访问 * * * * * >[info]#### 1. 判断访问是不是手机访问【is_mobile】 ~~~ /** * 判断访问是不是手机访问 * @return number */ function is_mobile(){ // returns true if one of the specified mobile browsers is detected // 如果监测到是指定的浏览器之一则返回true $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|"; $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|"; $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|"; $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|"; $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220"; $regex_match.=")/i"; // preg_match()方法功能为匹配字符,既第二个参数所含字符是否包含第一个参数所含字符,包含则返回1既true return preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT'])); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ echo is_mobile(); ~~~ - #### 函数结果 ~~~ 1 ~~~ * * * * * * * * * * * * * * * - ## 获取Url地址中的参数及参数值 * * * * * >[info]#### 1. 获取Url地址中的参数及参数值【get_url_key_value】 ~~~ /** * 获取Url地址中的参数及参数值 * @param unknown $url url地址 * @return multitype:NULL 返回参数数组 */ function get_url_key_value($url) { $result = array(); $mr = preg_match_all('/(\?|&)(.+?)=([^&?]*)/i', $url, $matchs); if ($mr !== FALSE) { for ($i = 0; $i < $mr; $i++) { $result[$matchs[2][$i]] = $matchs[3][$i]; } } return $result; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $url = 'http://www.baidu.com/index.php?m=content&c=index&a=lists&catid=6&area=0&author=0&h=0®ion=0&s=1&page=1'; echo "<pre>"; print_r(getUrlKeyValue($url)); echo "</pre>"; ~~~ - #### 函数结果 ~~~ Array ( [m] => content [c] => index [a] => lists [catid] => 6 [area] => 0 [author] => 0 [h] => 0®ion=0 [s] => 1 [page] => 1 ) ~~~ * * * * * * * * * * * * * * * - ## $b相对于$a的相对路径 * * * * * >[info]#### 1. $b相对于$a的相对路径【get_relatively_path】 ~~~ /*** * * @param unknown $a 路径一 * @param unknown $b 路径二 * @return string 返回路径二相对路径一的相对地址 */ function get_relatively_path($a,$b){ //拆分成数组 $a = explode('/',$a); $b = explode('/',$b); $path = ''; //将两个数组的索引重置 $c = array_values(array_diff($a,$b)); $d = array_values(array_diff($b,$a)); //去除掉a路径的文件名 array_pop($c); //将a路径中的目录名替换为.. foreach($c as &$v) $v = '..'; //合并两个数组 $e = array_merge($c,$d); //拼接路径 foreach($e as &$v) $path .= $v.'/'; return rtrim($path,'/'); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $a = '/a/b/c/d/e/f.php'; $b = '/a/b/12/34/c.php'; echo get_relatively_path($a,$b); ~~~ - #### 函数结果 ~~~ ../../../12/34/c.php ~~~ * * * * * * * * * * * * * * * - ## 二分法查找元素 * * * * * >[info]#### 1. 二分法查找元素【binary_search】 ~~~ /** * 二分法查找元素 * @param unknown $array 所查数组 * @param unknown $search 所查数 * @return number|boolean 返回查找数的索引 */ function binary_search($array, $search) { $low = 0; $high = count($array) - 1; while ($low <= $high) { $mid = floor(($low + $high) / 2); #找到元素 if ($search == $array[$mid]) { return $mid; } #元素比目标小,查找右部 if ($search > $array[$mid]) { $low = $mid + 1; } #元素比目标大,查找左部 if ($search < $array[$mid]) { $high = $mid - 1; } } #查找失败 return false; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $array=[1,2,3,4,5]; print_r(binary_search($array, 5)); ~~~ - #### 函数结果 ~~~ 4 ~~~ * * * * * * * * * * * * * * * - ## 返回输入数组中某个单一列的值 * * * * * >[info]#### 1. 返回输入数组中某个单一列的值【array_column】 ~~~ /** * 返回输入数组中某个单一列的值(该方法兼容php5.5版本以下,php5.5新增) * @date: 2017年8月8日 上午11:50:33 * $input array 必需。规定要使用的多维数组(记录集)。 * $columnKey 必需。需要返回值的列。 可以是索引数组的列的整数索引,或者是关联数组的列的字符串键值。 * $indexKey 可选。用作返回数组的索引/键的列。该参数也可以是 NULL,此时将返回整个数组(配合 $indexKey 参数来重置数组键的时候,非常有用)。 * @author: ityangs<ityangs@163.com> */ if( ! function_exists('array_column')) { function array_column($input, $columnKey, $indexKey = NULL) { $columnKeyIsNumber = (is_numeric($columnKey)) ? TRUE : FALSE; $indexKeyIsNull = (is_null($indexKey)) ? TRUE : FALSE; $indexKeyIsNumber = (is_numeric($indexKey)) ? TRUE : FALSE; $result = array(); foreach ((array)$input AS $key => $row) { if ($columnKeyIsNumber) { $tmp = array_slice($row, $columnKey, 1); $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : NULL; } else { $tmp = isset($row[$columnKey]) ? $row[$columnKey] : NULL; } if ( ! $indexKeyIsNull) { if ($indexKeyIsNumber) { $key = array_slice($row, $indexKey, 1); $key = (is_array($key) && ! empty($key)) ? current($key) : NULL; $key = is_null($key) ? 0 : $key; } else { $key = isset($row[$indexKey]) ? $row[$indexKey] : 0; } } $result[$key] = $tmp; } return $result; } } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ 从记录集中取出 last_name 列,用相应的 "id" 列作为键值: <?php // 表示由数据库返回的可能记录集的数组 $a = array( array( 'id' => 5698, 'first_name' => 'Bill', 'last_name' => 'Gates', ), array( 'id' => 4767, 'first_name' => 'Steve', 'last_name' => 'Jobs', ) array( 'id' => 3809, 'first_name' => 'Mark', 'last_name' => 'Zuckerberg', ) ); $last_names = array_column($a, 'last_name', 'id'); print_r($last_names); ?> ~~~ - #### 函数结果 ~~~ 输出: Array ( [5698] => Gates [4767] => Jobs [3809] => Zuckerberg ) ~~~ * * * * * * * * * * * * * * * - ## 根据参数获取数据表列 * * * * * >[info]#### 1. 根据参数获取数据表列【get_express_info】 ~~~ /** * 获取物流方式信息 * @param: $fileds String 查询字段(字符串) 'id,name' * @param: $params array 查询条件 * @return:return_type * @date: 2017年8月8日 下午6:03:36 * @author: ityangs<ityangs@163.com> */ public function get_express_info($fileds,$id){ $filed=''; $where='where 1=1 '; if(!empty($fileds)&& is_string($fileds)){ $filed=$fileds; }else $filed='*'; if(!empty($id)){ $where.="AND id={$id}"; } $sql = "select {$fileds} from express_infos {$where} order by id desc limit 1"; $query = $this->db->query($sql); $rs = $query->row_array(); return $rs; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $express_infos= $this->express_infos_model->get_express_info('express_method,send_type',$data['express_info_id']); ~~~ * * * * * * * * * * * * * * * - ## 多维数组转化为一维数组 * * * * * >[info]#### 1. 多维数组转化为一维数组【array_multi2single】 ~~~ /** * 多维数组转化为一维数组 * @param: array $array 多维数组 * @return:array $result_array 一维数组 * @date: 2017年8月10日 上午9:22:10 * @author: ityangs<ityangs@163.com> */ function array_multi2single($array) { $arr = array(); foreach ($array as $key => $val) { if (is_array($val)) { $arr = array_merge($arr, array_multi2single($val)); } else { $arr[] = $val; } } return $arr; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $multi = array( array( array( 'wo', 'shi' ), 'it' ), 'cheng', array( array( 'cheng', 'cheng' ) ), '!' ); $multi = array_multi2single($multi); print_r($multi); ~~~ - #### 函数结果 ~~~ 输出: Array ( [0] => wo [1] => shi [2] => it [3] => cheng [4] => cheng [5] => cheng [6] => ! ) ~~~ * * * * * * * * * * * * * * * - ## 记录日志函数 * * * * * >[info]#### 1. 记录日志函数【write_log】 ~~~ /** * 记录日志函数 * @param string $dirname 日志存放目录名称 * @param string $filename 日志文件名称,系统会自动在文件名称前加上当前日期 * @param string $log_msg 日志内容 * @date: 2017年8月14日 上午11:07:25 * @author: ityangs<ityangs@163.com> */ function write_log($dirname,$filename,$log_msg=''){ $url = ''; if(!empty($_SERVER['REQUEST_URI'])){ $url = $_SERVER['REQUEST_URI']; }else{ if(is_array($_SERVER['argv'])){ $url = implode("/", $_SERVER['argv']); } } if(is_dir('log') == false){ mkdir('log', 0777); } $log_path = 'log/'; //日志根目录 $month = date('Y_m'); //当前月份 $day = date('Y_m_d'); //当前日期 $now = date('Y-m-d H:i:s'); $user = empty($_SESSION['userinfo']['username'])?'System':$_SESSION['userinfo']['username']; $log_no = time(); if( empty($dirname) ){ $dirname = 'undefined'; } if( empty($filename) ){ $filename = 'undefined'; } if(is_dir($log_path.$month) == false){ mkdir($log_path.$month, 0777); } if(is_dir($log_path.$month.'/'.$dirname) == false){ mkdir($log_path.$month.'/'.$dirname, 0777); } $log_content = "编号:{$log_no}\n"; $log_content .= "[时间]{$now}\t[url]{$url}\t[操作人]{$user}\t\n[日志内容]\n"; $log_content .= print_r($log_msg,true)."\n\n"; $log_content .= "--------- End({$log_no}) ---------\n\n"; $path = $log_path.$month.'/'.$dirname.'/'.$day.'_'.$filename.'.txt'; file_put_contents($path,$log_content,FILE_APPEND); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $data=array( 'test1'=>'test1', 'test2'=>'test1', 'test3'=>'test3', 'test4'=>'test4', 'test5'=>'test5', ); write_log("logdir","log","result : ".json_encode($data)."\n日志结束!"); ~~~ - #### 函数结果 ~~~ 文件输出: 编号:1502680495 [时间]2017-08-14 11:14:55 [url]/test/log.php [操作人]System [日志内容] result : {"test1":"test1","test2":"test1","test3":"test3","test4":"test4","test5":"test5"} 日志结束! --------- End(1502680495) --------- ~~~ * * * * * * * * * * * * * * * - ## 自动去除空格以及将所有字母转换为大写 * * * * * >[info]#### 1. 自动去除空格以及将所有字母转换为大写【auto_convert_format】 ~~~ /** * 自动去除空格以及将所有字母转换为大写 * @param $string 待处理的字符串 */ function auto_convert_format( $string ){ $result = strtoupper( $string ); /***** 去除所有的前后空格(包括中文、全角下的空格) *****/ $pattern = '/(^[(\xc2\xa0)|\s| | | ]+)|([(\xc2\xa0)|\s| | | ]+$)/'; $result = preg_replace($pattern,'',$result); return $result; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ echo auto_convert_format("dsaas as dasd sf \s\n sdfbfdbfs"); ~~~ - #### 函数结果 ~~~ DSAAS AS DASD SF \S SDFBFDBFS ~~~ * * * * * * * * * * * * * * * - ## URL重定向函数 * * * * * >[info]#### 1. URL重定向函数【my_redirect】 ~~~ /** * URL重定向函数 * @param string $msg 提示语 * @param string $url 跳转地址,默认跳到前一页 */ function my_redirect($msg='',$url='') { $forward=!empty($url)?"location.href='{$url}'":'window.history.go(-1)'; $url = !empty($url)?$url:'前一页'; $msg = !empty($msg)?$msg:"系统将自动跳转到{$url}!"; echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; echo "<script type='text/javascript'>alert('{$msg}');{$forward}</script>"; exit; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ my_redirect('success','http://www.baidu.com'); my_redirect(); ~~~ * * * * * * * * * * * * * * * - ## RTX发送即时消息 * * * * * >[info]#### 1. RTX发送即时消息【send_rtx_message】 ~~~ /** * RTX发送即时消息 * @access public * @param string $users 接收消息的RTX号码,多人接收消息时,可传数组,内部将会用逗号分如1288,1289,1290... * @param string $msg_subject 消息标题 * @param string $msg_body 消息内容 * 注: * 1、内容中的http地址:编号为:{$task_val['batch_no']} 的任务生成文件失败,[请知悉|{$url}] */ function send_rtx_message($users,$msg_subject,$msg_body){ if (is_array($users)){ $users = implode(',',$users); } if(strlen($msg_body)>900){ $msg_body = mb_substr($msg_body,-900,900, "utf-8"); } $msg_subject = iconv("UTF-8", "GBK", $msg_subject); $msg_body = iconv("UTF-8", "GBK", $msg_body); $url = "http://58.251.136.63:101011/api_wu.php"; $post_data = array ( "data" => "c695ab0ac9e43afc6f1ce8b724e09bf0a0bwuc".$msg_subject."a0bwuc".$users."a0bwuc".$msg_body ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch); curl_close($ch); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ send_rtx_message('13699', "test", "test"); ~~~ * * * * * * * * * * * * * * * - ## CURL远程访问并随机更换国内的IP地址 * * * * * >[info]#### 1. CURL远程访问并随机更换国内的IP地址【curl_get_contents】 ~~~ /** * 随机提供了国内的IP地址 * @return multitype:string */ function get_rand_ip(){ $ip_long = array( array('607649792', '608174079'), //36.56.0.0-36.63.255.255 array('1038614528', '1039007743'), //61.232.0.0-61.237.255.255 array('1783627776', '1784676351'), //106.80.0.0-106.95.255.255 array('2035023872', '2035154943'), //121.76.0.0-121.77.255.255 array('2078801920', '2079064063'), //123.232.0.0-123.235.255.255 array('-1950089216', '-1948778497'), //139.196.0.0-139.215.255.255 array('-1425539072', '-1425014785'), //171.8.0.0-171.15.255.255 array('-1236271104', '-1235419137'), //182.80.0.0-182.92.255.255 array('-770113536', '-768606209'), //210.25.0.0-210.47.255.255 array('-569376768', '-564133889'), //222.16.0.0-222.95.255.255 ); $rand_key = mt_rand(0, 9); $ip= long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1])); $headers['CLIENT-IP'] = $ip; $headers['X-FORWARDED-FOR'] = $ip; $headerArr = array(); foreach( $headers as $n => $v ) { $headerArr[] = $n .':' . $v; } return $headerArr; } /** * curl远程获取页面 * @param unknown $url URL地址 * @return mixed */ function curl_get_contents($url) { $headers = get_rand_ip(); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_BINARYTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0"); //模拟浏览器类型 curl_setopt($curl, CURLOPT_TIMEOUT, 100); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $tmpInfo = curl_exec($curl); if (curl_errno($curl)) { print "Error: " . curl_error($curl); } else { curl_close($curl); } return $tmpInfo; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ curl_get_contents("http://127.0.0.1/test/tests.php"); ~~~ * * * * * * * * * * * * * * * - ## RTX 报警给开发人员 * * * * * >[info]#### 1. RTX 报警给开发人员【sendRtx】 ~~~ <?php /** * RTX发送即时消息 * * @access public * @param string $users * 接收消息的RTX号码,多人接收消息时,可传数组,内部将会用逗号分如1288,1289,1290... * @param string $msg_subject * 消息标题 * @param string $msg_body * 消息内容 */ function send_rtx_message($users, $msg_subject, $msg_body) { if (is_array($users)) { $users = implode(',', $users); } if (strlen($msg_body) > 900) { $msg_body = mb_substr($msg_body, - 900, 900, "utf-8"); } $msg_subject = iconv("UTF-8", "GBK", $msg_subject); $msg_body = iconv("UTF-8", "GBK", $msg_body); $url = "http://58.251.136.63:101011/api_wu.php"; $post_data = array( "data" => "c695ab0ac9e43afc6f1ce8b724e09bf0a0bwuc" . $msg_subject . "a0bwuc" . $users . "a0bwuc" . $msg_body ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch); curl_close($ch); } /** * RTX 报警给开发人员 * * @param array|String $users * 接收消息的RTX号码,多人接收消息时,可传字符串,用逗号分如1288,1289,1290..., * @param unknown $msg_subject * 消息标题 * @param unknown $msg_body * 消息内容 * @param unknown $msg_code * 错误简码,多种错误类型时,可传字符串,用逗号分如10001,10002,10003..., * @date: 2017年8月29日 下午3:17:23 * @author : ityangs<ityangs@163.com> * 使用方式:在系统中使用sendRtx($rtx)函数调用此函数 * $msg_subject = "【出口WMS系统】小包接口异常通知"; * sendRtxToDeveloper('136999,136998', $msg_subject, 'tesd错误详情错误详错误详错误详错误详错误详错误详错误详错误详错误详错', '100002,100011,100005,100011,1006'); * */ function sendRtxToDeveloper($users, $msg_subject, $msg_body, $msg_code) { $config_rtx = array( 'msg_code' => array( 'P0' => array( '100001' => 'PHP致命错误1', '100002' => 'PHP致命错误2' ), 'P1' => array( '100003' => 'PHP致命错误3', '100004' => 'PHP致命错误4', '100005' => 'PHP致命错误5' ), 'P2' => array( '100006' => 'PHP致命错误6' ), 'P3' => array( '100007' => 'PHP致命错误7' ), 'P4' => array( '100008' => 'PHP致命错误8' ), 'P5' => array( '100009' => 'PHP致命错误9' ), 'P6' => array( '100010' => 'PHP致命错误10', '100011' => 'PHP致命错误11' ), 'P7' => array( '100012' => 'PHP致命错误12', '100013' => 'PHP致命错误13' ) ), 'rtx_user' => array( '13699' => array( 'name' => 'ityangs', 'accept_code' => array( '100001', '100002', '100003' ) ), '13700' => array( 'name' => 'ityangs1', 'accept_code' => array( '100001', '100002', '100004' ) ) ) ); /* 错误代码code信息 */ $msg_code_arrs = $config_rtx['msg_code']; // msg_code错误级别和错误信息 $rtx_user_arrs = $config_rtx['rtx_user']; $code_arrs = array(); // 错误简码与级别数组:1000001=>P1 foreach ($msg_code_arrs as $k1 => $v1) { foreach ($v1 as $k2 => $v2) { $code_arrs[$k2] = $k1; } } /* 获取最高的错误级别 */ $level_str = '无'; $msg_code_str = '无'; // 错误简码 $msg_details_str = '无'; // 错误详情 $msg_code_arr = array(); if (is_string($msg_code) && ! empty($msg_code)) { $msg_code_arr = array_unique(explode(',', $msg_code)); sort($msg_code_arr); // 错误简码排序 $level_arr = array(); if (count($msg_code_arr) > 0) { foreach ($msg_code_arr as $key => $val) { if (! array_key_exists($val, $code_arrs)) { continue; } $level_arr[] = $code_arrs[$val]; $msg_code_str .= "{$val} ,"; $msg_details_str .= "{$msg_code_arrs[$code_arrs[$val]][$val]} ,"; } } sort($level_arr); $level_str = $level_arr[0]; // 取最高发错误级别 $msg_code_str = rtrim($msg_code_str, ','); $msg_details_str = rtrim($msg_details_str, ','); } $user_str = ''; $user_arr = array(); if (is_string($users) && ! empty($msg_code)) { $user_arr = array_unique(explode(',', $users)); if (count($user_arr) > 0) { foreach ($user_arr as $key => $val) { if (! array_key_exists($val, $rtx_user_arrs) || ! array_intersect((array) $rtx_user_arrs[$val]['accept_code'], $msg_code_arr)) { continue; } $user_str .= $rtx_user_arrs[$val]['name'] . "($val) ,"; } } $user_str = rtrim($user_str, ','); } $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; if ($user_str != '') { $msg_content = "【发生时间】:" . date('Y-m-d H:i:s') . "\n"; $msg_content .= "【错误级别】:".($level_str?:'无')."\n"; $msg_content .= "【错误简码】:".($msg_code_str?:'无')."\n"; $msg_content .= "【错误详情】:".($msg_details_str?:'无')."\n"; $msg_content .= "【已通知人】:{$user_str}\n"; $msg_content .= "【报警内容】" . $msg_body . " 请及时处理![访问来源]:{$url}\n"; send_rtx_message($users, $msg_subject, $msg_content); } } /** * RTX报警给相关的人 * * @param array $rtx * rtx信息 * 使用方式: * $rtx=array( * array( * 'users'=>'13699,13689',//要通知的人员的RTX号码,字符串型,必填 * 'msg_subject'=>'【出口WMS】接口通知',//要通知的信息的标题,字符串型,必填 * 'msg_body'=>'接口因网络原因中断',//要通知详细内容,字符串型,必填 * 'msg_code'=>'100001,100002,100003',//报警的错误码,必须填系统配置里的错误码,根据错误码找错误等级和是否发送的人员,字符串型,必填 * ), * array( * 'users'=>'13696,13689',//要通知的人员的RTX号码,字符串型,必填 * 'msg_subject'=>'【出口WMS】接口通知2',//要通知的信息的标题,字符串型,必填 * 'msg_body'=>'接口因网络原因中断2',//要通知详细内容,字符串型,必填 * 'msg_code'=>'100005,100007,100011',//报警的错误码,必须填系统配置里的错误码,根据错误码找错误等级和是否发送的人员,字符串型,必填 * ), * ); * sendRtx($rtx); */ function sendRtx($rtx = array()) { if (! $rtx) { return false; } if(isset($rtx['users'])&&!empty($rtx['users'])){ $users = $rtx['users'] ? : ''; // 为空不报警 $msg_subject = $rtx['msg_subject'] ? : ''; $msg_body = $rtx['msg_body'] ? : ''; $msg_code = $rtx['msg_code'] ? : ''; $this->sendRtxToDeveloper($users, $msg_subject, $msg_body, $msg_code); } foreach ($rtx as $key => $val) { $users = $val['users'] ? : ''; // 为空不报警 $msg_subject = $val['msg_subject'] ? : ''; $msg_body = $val['msg_body'] ? : ''; $msg_code = $val['msg_code'] ? : ''; sendRtxToDeveloper($users, $msg_subject, $msg_body, $msg_code); } } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $rtx = array( array( 'users' => '13699,13689', // 要通知的人员的RTX号码,字符串型,必填 'msg_subject' => '【出口WMS】接口通知', // 要通知的信息的标题,字符串型,必填 'msg_body' => '接口因网络原因中断', // 要通知详细内容,字符串型,必填 'msg_code' => '100001,100002,100003' ) // 报警的错误码,必须填系统配置里的错误码,根据错误码找错误等级和是否发送的人员,字符串型,必填 , array( 'users' => '13696,13699', // 要通知的人员的RTX号码,字符串型,必填 'msg_subject' => '【出口WMS】接口通知2', // 要通知的信息的标题,字符串型,必填 'msg_body' => '接口因网络原因中断2', // 要通知详细内容,字符串型,必填 'msg_code' => '100005,100002,100011' ) // 报警的错误码,必须填系统配置里的错误码,根据错误码找错误等级和是否发送的人员,字符串型,必填 ); sendRtx($rtx); ~~~ * * * * * * * * * * * * * * * - ## 对称加密算法 * * * * * >[info]#### 1.对称加密算法【encode_str,decode_str】 ~~~ <?php /** * 对称加密算法之加密 * @param String $string 需要加密的字串 * @param String $skey 加密EKY * @author ityangs <ityangs@163.com> * @date 2017-09-07 10:30 * @return String */ function encode_str($string = '', $skey = 'cxphp') { $strArr = str_split(base64_encode($string)); $strCount = count($strArr); foreach (str_split($skey) as $key => $value) $key < $strCount && $strArr[$key].=$value; return str_replace(array('=', '+', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr)); } /** * 对称加密算法之解密 * @param String $string 需要解密的字串 * @param String $skey 解密KEY * @author ityangs <ityangs@163.com> * @date 2017-09-07 10:30 * @return String */ function decode_str($string = '', $skey = 'cxphp') { $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', '+', '/'), $string), 2); $strCount = count($strArr); foreach (str_split($skey) as $key => $value) $key <= $strCount && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0]; return base64_decode(join('', $strArr)); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $str = '56,15dasdasd65247,54风格sssss \r\s\sd\n'; echo "string : " . $str . " <br />"; echo "encode : " . ($enstring = str_encode($str)) . '<br />'; echo "decode : " . str_decode($enstring); ~~~ - #### 函数结果 ~~~ string : 56,15dasdasd65247,54风格sssss \r\s\sd\n encode : NcTxYpshMpTVkYXNkYXNkNjUyNDcsNTTpo47moLxzc3NzcyBcclxzXHNkXG4O0O0O decode : 56,15dasdasd65247,54风格sssss \r\s\sd\n ~~~ * * * * * * * * * * * * * * * - ## 安全URL编码、解码 * * * * * >[info]#### 1.安全URL编码、解码【encode_url,decode_url】 ~~~ /** * 安全URL编码 * @param string $data * @author ityangs <ityangs@163.com> * @date 2017-09-07 10:30 * @return string */ function encode_url($url) { return str_replace(array('+', '/', '='), array('-', '_', ''), base64_encode(serialize($url))); } /** * 安全URL解码 * @param string $string * @author ityangs <ityangs@163.com> * @date 2017-09-07 10:30 * @return string */ function decode_url($url) { $data = str_replace(array('-', '_'), array('+', '/'), $url); $mod4 = strlen($data) % 4; ($mod4) && $data .= substr('====', $mod4); return unserialize(base64_decode($data)); } $url = 'https://www.kancloud.cn/book/ityangs/phpcode/edit#RTX__1709'; echo "url : " . $url . " <br />"; echo "encode : " . ($enstring = encode_url($url)) . '<br />'; echo "decode : " . decode_url($enstring); ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $url = 'https://www.kancloud.cn/book/ityangs/phpcode/edit#RTX__1709'; echo "url : " . $url . " <br />"; echo "encode : " . ($enstring = encode_url($url)) . '<br />'; echo "decode : " . decode_url($enstring); ~~~ - #### 函数结果 ~~~ url : https://www.kancloud.cn/book/ityangs/phpcode/edit#RTX__1709 encode : czo1OToiaHR0cHM6Ly93d3cua2FuY2xvdWQuY24vYm9vay9pdHlhbmdzL3BocGNvZGUvZWRpdCNSVFhfXzE3MDkiOw decode : https://www.kancloud.cn/book/ityangs/phpcode/edit#RTX__1709 ~~~ * * * * * * * * * * * * * * * - ## 浏览器友好的变量输出 * * * * * >[info]#### 1.URL重定向【redirect】 ~~~ /** * URL重定向 * @param string $url 重定向的URL地址 * @param integer $time 重定向的等待时间(秒) * @param string $msg 重定向前的提示信息 * @return void */ function redirect($url, $time=0, $msg='') { //多行URL地址支持 $url = str_replace(array("\n", "\r"), '', $url); if (empty($msg)) $msg = "系统将在{$time}秒之后自动跳转到{$url}!"; if (!headers_sent()) { // redirect if (0 === $time) { header('Location: ' . $url); } else { header("refresh:{$time};url={$url}"); echo($msg); } exit(); } else { $str = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>"; if ($time != 0) $str .= $msg; exit($str); } } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $url = 'https://www.kancloud.cn/book/ityangs/phpcode/edit#RTX__1709'; echo "url : " . $url . " <br />"; echo "encode : " . ($enstring = encode_url($url)) . '<br />'; echo "decode : " . decode_url($enstring); ~~~ - #### 函数结果 ~~~ url : https://www.kancloud.cn/book/ityangs/phpcode/edit#RTX__1709 encode : czo1OToiaHR0cHM6Ly93d3cua2FuY2xvdWQuY24vYm9vay9pdHlhbmdzL3BocGNvZGUvZWRpdCNSVFhfXzE3MDkiOw decode : https://www.kancloud.cn/book/ityangs/phpcode/edit#RTX__1709 ~~~ * * * * * * * * * * * * * * * - ## 获取单表信息 * * * * * >[info]#### 1.获取单表信息【getTableInfos】 ~~~ /** * 获取单表信息 * @param $column array 获取表的列,空数组为所有列 * @date: 2017年10月11日 上午10:09:51 * @author: ityangs<ityangs@163.com> */ public function getTableInfos($table,$column=array('id'),$where=" where 1=1 "){ $column = $column?implode(',',$column):'*'; $sql = "select {$column} from {$table} {$where} "; $query = $this->db->query($sql); return $query->result_array(); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ getTableInfos('a',array()); ~~~ * * * * * * * * * * * * * * * - ## 获取请求参数 * * * * * >[info]#### 1.获取请求参数【getParames】 ~~~ /** * 获取请求参数(浏览器和cli都可以) * @param: $options 该字符串中的每个字符会被当做选项字符,匹配传入脚本的选项以单个连字符(-)开头。 比如,一个选项字符串 "x" 识别了一个选项 -x。 只允许 a-z、A-Z 和 0-9。 * @param: $longopts 选项数组。此数组中的每个元素会被作为选项字符串,匹配了以两个连字符(--)传入到脚本的选项。 例如,长选项元素 "opt" 识别了一个选项 --opt。 * @return:return_type * @date: 2017年10月12日 下午5:16:51 * @author: ityangs<ityangs@163.com> * $options='a:b:c:d::e'; php test.php -a 1 -b 2 -c 3 -d=4 -e 5 * $longopts = array(//php test.php --type news --is_hot 1 --limit=10 --expire=100 'type:', 'is_hot:', 'limit::', 'expire' ); */ public function getParames($options='',$longopts=array()){ $result=array(); if(!$this->is_cli){ $result=$_REQUEST; }else{ $result=!empty($options)||!$longopts?getopt($options, $longopts):array(); } return $result; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $params=$this->getParames('', array('num:','start_time:','end_time:')); print_r($params); ~~~ - #### 函数结果 ~~~ D:\phpStudy\WWW\office\yun.wms\test\daemon\lms>php sys_shipment_clearance_prepare.php --type=a --start_time=2017-01-02 <pre>Array ( [type] => a [start_time] => 2017-01-02 ) </pre> ~~~ * * * * * * * * * * * * * * * - ## 自定义进一规则(尾数为1-9的倍数) * * * * * >[info]#### 1.自定义进一规则(尾数为1-9的倍数)【getParames】 ~~~ /** * 自定义进一规则(尾数为1-9的倍数) */ function ceiling($number, $significance = 1) { return ( is_numeric($number) && is_numeric($significance) ) ? (ceil($number/$significance)*$significance) : 0; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ print_r(ceiling(1.514,0.8)); //1.6 print_r(ceiling(1.514,0.5)); //2 print_r(ceiling(1.4514,0.5)); //1.5 ~~~ - #### 函数结果 ~~~ 1.6 2 1.5 ~~~ - ## 获取任务类型无限极分类Option * * * * * >[info]#### 1.获取任务类型无限极分类Option【getTaskTypeTree】 ~~~ 表结构: CREATE TABLE `relative_datas_export_task_type` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL DEFAULT '' COMMENT '类型名称', `pid` int(11) NOT NULL DEFAULT '-1' COMMENT '父级id(顶级为0)', `create_time` datetime NOT NULL DEFAULT '0000-01-01 00:00:00' COMMENT '创建时间', `create_user` varchar(30) NOT NULL DEFAULT '' COMMENT '创建人', `update_time` datetime NOT NULL DEFAULT '0000-01-01 00:00:00' COMMENT '更新时间', `update_user` varchar(30) NOT NULL DEFAULT '' COMMENT '更新人', `is_delete` tinyint(4) NOT NULL DEFAULT '2' COMMENT '任务记录是否删除: 1:是 2:否', `is_disable` tinyint(4) NOT NULL DEFAULT '2' COMMENT '分类是否禁用 1:禁用 2:不禁用', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='相关数据导出任务类型表'; /** * 获取任务类型无限极分类Option * @param: $selected 选中的option * @return: return_type * @date: 2018年1月23日 下午5:03:00 * @author: ityangs<ityangs@163.com> */ function getTaskTypeTree($selected=''){ $type_data=$this->relative_datas_export_task_model->getTaskType(); $data=$this->createTreeArr($type_data, $pk='id', $pid = 'pid', $child = 'child', $level = 0); return $this->getOption($data,$child = 'child','&nbsp;&nbsp;',false,$selected); } /** * 把返回的数据集转换成Tree数组 * @param array $list 要转换的数据集 * @param string $pid parent标记字段 * @param string $child child标记字段 * @param string $level level标记字段 * @return array * @author: ityangs<ityangs@163.com> */ function createTreeArr($list, $pk='id', $pid = 'pid', $child = 'child', $level = 0) { // 创建Tree $tree = array(); if(is_array($list)) { // 创建基于主键的数组引用 $refer = array(); foreach ($list as $key => $data) { $refer[$data[$pk]] =& $list[$key]; } foreach ($list as $key => $data) { // 判断是否存在parent $parentId = $data[$pid]; if ($level == $parentId) { $tree[] =& $list[$key]; }else{ if (isset($refer[$parentId])) { $parent =& $refer[$parentId]; $parent[$child][] =& $list[$key]; } } } } return $tree; } /** * 显示下拉框的树. * @param array $list 数据数组. * @param string $child child标记字段 * @param string $prefix 前缀 * @param string $select 选中的option * @return string. * @author: ityangs<ityangs@163.com> */ function getOption($list, $child = 'child',$prefix='|---',$flag=false,$selected=''){ foreach($list as $row) { $tmp_prefix.=$prefix; $prefix=$flag?$prefix:''; $select=$selected==$row['id']?'selected':''; $str .= '<option value="' . $row['id'] . '" '.$select.'>'. $prefix . $row['name'] . '</option>'; if(isset($row[$child])){ $tmp_prefix.=$tmp_prefix; $str .=$this->getOption($row[$child],$child,$tmp_prefix,true,$selected); }else { $tmp_prefix=''; continue; } } return $str; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $this->getTaskTypeTree($params['type_id']) ~~~ - #### 函数结果 ~~~ <select class="form-control" name="type_id"> <option value="">任务类型</option> <option value="1">发货管理</option> <option value="2" selected>&nbsp;&nbsp;&nbsp;&nbsp;库存管理</option> <option value="3">入库管理</option> </select> ~~~ * * * * * * * * * * * * * * * - ## 下划线命名与驼峰命名互转 * * * * * >[info]#### 1.下划线命名与驼峰命名互转【underline2camelize,camelize2underline】 ~~~ /** * 下划线命名转驼峰命名 * 思路: * step1.原字符串转小写,原字符串中的分隔符用空格替换,在字符串开头加上分隔符 * step2.将字符串中每个单词的首字母转换为大写,再去空格,去字符串首部附加的分隔符. * @date: 2018年1月25日 上午10:51:38 * @author: ityangs<ityangs@163.com> */ public function underline2camelize($words,$separator='_') { $words = $separator. str_replace($separator, " ", strtolower($words)); return ltrim(str_replace(" ", "", ucwords($words)), $separator ); } /** * 驼峰命名转下划线命名 * 思路:小写和大写紧挨一起的地方,加上分隔符,然后全部转小写 * @date: 2018年1月25日 上午10:51:38 * @author: ityangs<ityangs@163.com> */ public function camelize2underline($words,$separator='_') { return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $words)); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ underline2camelize(a_b_c); camelize2underline(aBC); ~~~ - #### 函数结果 ~~~ aBc a_b_c ~~~ * * * * * * * * * * * * * * * - ## 多个连续空格只保留一个 * * * * * >[info]#### 1.多个连续空格只保留一个【merge_spaces】 ~~~ /** * 多个连续空格只保留一个 * * @param string $string 待转换的字符串 * @return unknown */ static public function merge_spaces ( $string ) { return preg_replace ( "/\s(?=\s)/","\\1", $string ); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ merge_spaces('sadsa asdsdas dadsa d ddd') ~~~ - #### 函数结果 ~~~ sadsa asdsdas dadsa d ddd ~~~ * * * * * * * * * * * * * * * - ## 批量生成邀请码 * * * * * >[info]#### 1.批量生成邀请码【createApplyNum】 ~~~ /** * 批量新增邀请码 */ protected function createApplyNum($num, $amount, $prefix, $is_number=true) { $numArr = range(0, 9); // 数字 $numAndLetterArr = array_merge($numArr, range('A', 'Z')); // 字母+数字 $applynumArr = array(); for ($i = 0; $i < $amount; $i ++) { $tempNumStr = ''; if ($is_number) { // 是数字 for ($j = 0; $j < $num; $j++) { $tempNumStr.=array_rand($numArr); } } else { // 是数字+字母 for ($j = 0; $j < $num; $j++) { $tempNumStr.=$numAndLetterArr[array_rand($numAndLetterArr)]; } } $applynumArr[$i]=strtoupper($prefix.$tempNumStr); } array_unique($applynumArr); return $applynumArr; } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ $applyNumArr=$this->createApplyNum($num, $amount, $prefix, $is_number); ~~~ - #### 函数结果 ~~~ Array ( [0] => 47766 [1] => 86826 ) ~~~ * * * * * * * * * * * * * * * - ## 常返回弹出提醒 * * * * * >[info]#### 1.常返回弹出提醒【go_back】 ~~~ /** 异常返回弹出提醒 *@param string $msg 提示信息 *@param string $url 相对路径 提醒完成后跳转到指定位置 *@param int $refresh 返回是否刷新 0 否 1 是 * */ function go_back($msg = '操作异常', $url='', $refresh = 0) { echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; if ($url == '') { $refresh_type = $refresh == 1 ? 'history.back(-1)' : 'history.go(-1)'; echo "<script type='text/javascript'>alert('".$msg."');{$refresh_type};</script>"; } else { echo "<script type='text/javascript'>alert('".$msg."');window.location.href='".$url."';</script>"; } exit(); } ~~~ >[info]#### 2. 测试 - #### 函数使用 ~~~ go_back(); ~~~