您好,欢迎访问一九零五行业门户网

PHP常用工具类大全,工具类大全_PHP教程

php常用工具类大全,工具类大全params['emailhost']; //发送邮件服务器$mailer->port = yii::app()->params['emailport']; //邮件端口$mailer->timeout = yii::app()->params['emailtimeout'];//邮件发送超时时间$mailer->contenttype = 'text/html';//设置html格式$mailer->smtpauth = true;$mailer->username = yii::app()->params['emailusername'];$mailer->password = yii::app()->params['emailpassword'];$mailer->issmtp ();$mailer->from = $mailer->username; // 发件人邮箱$mailer->fromname = yii::app()->params['emailformname']; // 发件人姓名$mailer->addreplyto ( $mailer->username );$mailer->charset = 'utf-8';// 添加邮件日志$modelmail = new maillog ();$modelmail->accept = $toemail;$modelmail->subject = $subject;$modelmail->message = $message;$modelmail->send_status = 'waiting';$modelmail->save ();// 发送邮件$mailer->addaddress ( $toemail );$mailer->subject = $subject;$mailer->body = $message;if ($mailer->send () === true) {$modelmail->times = $modelmail->times + 1;$modelmail->send_status = 'success';$modelmail->save ();return true;} else {$error = $mailer->errorinfo;$modelmail->times = $modelmail->times + 1;$modelmail->send_status = 'failed';$modelmail->error = $error;$modelmail->save ();return false;}}/*** 判断字符串是utf-8 还是gb2312* @param unknown $str* @param string $default* @return string*/public static function utf8_gb2312($str, $default = 'gb2312'){ $str = preg_replace(/[\x01-\x7f]+/, , $str); if (empty($str)) return $default; $preg = array( gb2312 => /^([\xa1-\xf7][\xa0-\xfe])+$/, //正则判断是否是gb2312 utf-8 => /^[\x{4e00}-\x{9fa5}]+$/u, //正则判断是否是汉字(utf8编码的条件了),这个范围实际上已经包含了繁体中文字了 ); if ($default == 'gb2312') { $option = 'utf-8'; } else { $option = 'gb2312'; } if (!preg_match($preg[$default], $str)) { return $option; } $str = @iconv($default, $option, $str); //不能转成 $option, 说明原来的不是 $default if (empty($str)) { return $option; } return $default;}/*** utf-8和gb2312自动转化* @param unknown $string* @param string $outencoding* @return unknown|string*/public static function safeencoding($string,$outencoding = 'utf-8'){$encoding = utf-8;for($i = 0; $i = 1024 ) {$size /= 1024;$pos ++;}return round( $size, $dec ) . . $a[$pos];}/*** 下拉框,单选按钮 自动选择** @param $string 输入字符* @param $param 条件* @param $type 类型* selected checked* @return string*/static public function selected( $string, $param = 1, $type = 'select' ) {$true = false;if ( is_array( $param ) ) {$true = in_array( $string, $param );}elseif ( $string == $param ) {$true = true;}$return='';if ( $true )$return = $type == 'select' ? 'selected=selected' : 'checked=checked';echo $return;}/*** 下载远程图片* @param string $url 图片的绝对url* @param string $filepath 文件的完整路径(例如/www/images/test) ,此函数会自动根据图片url和http头信息确定图片的后缀名* @param string $filename 要保存的文件名(不含扩展名)* @return mixed 下载成功返回一个描述图片信息的数组,下载失败则返回false*/static public function downloadimage($url, $filepath, $filename) {//服务器返回的头信息$responseheaders = array();//原始图片名$originalfilename = '';//图片的后缀名$ext = '';$ch = curl_init($url);//设置curl_exec返回的值包含http头curl_setopt($ch, curlopt_header, 1);//设置curl_exec返回的值包含http内容curl_setopt($ch, curlopt_returntransfer, 1);//设置抓取跳转(http 301,302)后的页面curl_setopt($ch, curlopt_followlocation, 1);//设置最多的http重定向的数量curl_setopt($ch, curlopt_maxredirs, 3);//服务器返回的数据(包括http头信息和内容)$html = curl_exec($ch);//获取此次抓取的相关信息$httpinfo = curl_getinfo($ch);curl_close($ch);if ($html !== false) {//分离response的header和body,由于服务器可能使用了302跳转,所以此处需要将字符串分离为 2+跳转次数 个子串$httparr = explode(\r\n\r\n, $html, 2 + $httpinfo['redirect_count']);//倒数第二段是服务器最后一次response的http头$header = $httparr[count($httparr) - 2];//倒数第一段是服务器最后一次response的内容$body = $httparr[count($httparr) - 1];$header.=\r\n;//获取最后一次response的header信息preg_match_all('/([a-z0-9-_]+):\s*([^\r\n]+)\r\n/i', $header, $matches);if (!empty($matches) && count($matches) == 3 && !empty($matches[1]) && !empty($matches[1])) {for ($i = 0; $i $sizeinfo[1], 'orginalfilename' => $originalfilename, 'filename' => pathinfo($filepath, pathinfo_basename));}}}}return false;}/*** 查找ip是否在某个段位里面* @param string $ip 要查询的ip* @param $arrip 禁止的ip* @return boolean*/public static function ipaccess($ip='0.0.0.0', $arrip = array()){$access = true;$ip && $arr_cur_ip = explode('.', $ip);foreach((array)$arrip as $key=> $value){if($value == '*.*.*.*'){$access = false; //禁止所有break;}$tmp_arr = explode('.', $value);if(($arr_cur_ip[0] == $tmp_arr[0]) && ($arr_cur_ip[1] == $tmp_arr[1])) {//前两段相同if(($arr_cur_ip[2] == $tmp_arr[2]) || ($tmp_arr[2] == '*')){//第三段为* 或者相同if(($arr_cur_ip[3] == $tmp_arr[3]) || ($tmp_arr[3] == '*')){//第四段为* 或者相同$access = false; //在禁止ip列,则禁止访问break;}}}}return $access;}/*** @param string $string 原文或者密文* @param string $operation 操作(encode | decode), 默认为 decode* @param string $key 密钥* @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效* @return string 处理后的 原文或者 经过 base64_encode 处理后的密文** @example** $a = authcode('abc', 'encode', 'key');* $b = authcode($a, 'decode', 'key'); // $b(abc)** $a = authcode('abc', 'encode', 'key', 3600);* $b = authcode('abc', 'decode', 'key'); // 在一个小时内,$b(abc),否则 $b 为空*/public static function authcode($string, $operation = 'decode', $key = '', $expiry = 3600) {$ckey_length = 4;// 随机密钥长度 取值 0-32;// 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。// 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方// 当此值为 0 时,则不产生随机密钥$key = md5 ( $key ? $key : 'key' ); //这里可以填写默认key值$keya = md5 ( substr ( $key, 0, 16 ) );$keyb = md5 ( substr ( $key, 16, 16 ) );$keyc = $ckey_length ? ($operation == 'decode' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';$cryptkey = $keya . md5 ( $keya . $keyc );$key_length = strlen ( $cryptkey );$string = $operation == 'decode' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;$string_length = strlen ( $string );$result = '';$box = range ( 0, 255 );$rndkey = array ();for($i = 0; $i $filearr);}/*** 取得输入目录所包含的所有文件* 以数组形式返回* author: flynetcn*/static public function get_dir_files($dir){if (is_file($dir)) {return array($dir);}$files = array();if (is_dir($dir) && ($dir_p = opendir($dir))) {$ds = directory_separator;while (($filename = readdir($dir_p)) !== false) {if ($filename=='.' || $filename=='..') { continue; }$filetype = filetype($dir.$ds.$filename);if ($filetype == 'dir') {$files = array_merge($files, self::get_dir_files($dir.$ds.$filename));} elseif ($filetype == 'file') {$files[] = $dir.$ds.$filename;}}closedir($dir_p);}return $files;}}
太多1000多行呢了保存不了,查看完整并下载 
 下载地址:http://www.shouce.ren/post/d/id/1700
http://www.bkjia.com/phpjc/1078068.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1078068.htmltecharticlephp常用工具类大全,工具类大全 ?php/** * 助手类 * @author www.shouce.ren * */class helper{/*** 判断当前服务器系统* @return string*/public static function ge...
其它类似信息

推荐信息