自己项目中PHP常用工具类大全分享,php工具类大全分享_PHP教程
自己项目中PHP常用工具类大全分享,php工具类大全分享
Php代码
-
-
- class Helper
- {
-
- public static function getOS(){
- if(PATH_SEPARATOR == ':'){
- return 'Linux';
- }else{
- return 'Windows';
- }
- }
-
- public static function microtime_float() {
- list ( $usec, $sec ) = explode ( " ", microtime () );
- return (( float ) $usec + ( float ) $sec);
- }
-
- public static function truncate_utf8_string($string, $length, $etc = '...') {
- $result = '';
- $string = html_entity_decode ( trim ( strip_tags ( $string ) ), ENT_QUOTES, 'UTF-8' );
- $strlen = strlen ( $string );
- for($i = 0; (($i $strlen) && ($length > 0)); $i ++) {
- if ($number = strpos ( str_pad ( decbin ( ord ( substr ( $string, $i, 1 ) ) ), 8, '0', STR_PAD_LEFT ), '0' )) {
- if ($length
- break;
- }
- $result .= substr ( $string, $i, $number );
- $length -= 1.0;
- $i += $number - 1;
- } else {
- $result .= substr ( $string, $i, 1 );
- $length -= 0.5;
- }
- }
- $result = htmlspecialchars ( $result, ENT_QUOTES, 'UTF-8' );
- if ($i $strlen) {
- $result .= $etc;
- }
- return $result;
- }
-
- public static function scanfDir($dir='', $all = false, &$ret = array()){
- if ( false !== ($handle = opendir ( $dir ))) {
- while ( false !== ($file = readdir ( $handle )) ) {
- if (!in_array($file, array('.', '..', '.git', '.gitignore', '.svn', '.htaccess', '.buildpath','.project'))) {
- $cur_path = $dir . '/' . $file;
- if (is_dir ( $cur_path )) {
- $ret['dirs'][] =$cur_path;
- $all && self::scanfDir( $cur_path, $all, $ret);
- } else {
- $ret ['files'] [] = $cur_path;
- }
- }
- }
- closedir ( $handle );
- }
- return $ret;
- }
-
- public static function sendMail($toemail = '', $subject = '', $message = '') {
- $mailer = Yii::createComponent ( 'application.extensions.mailer.EMailer' );
-
- $mailer->SetLanguage('zh_cn');
- $mailer->Host = Yii::app()->params['emailHost'];
- $mailer->Port = Yii::app()->params['emailPort'];
- $mailer->Timeout = Yii::app()->params['emailTimeout'];
- $mailer->ContentType = 'text/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;
- }
- }
-
- public static function utf8_gb2312($str, $default = 'gb2312')
- {
- $str = preg_replace("/[\x01-\x7F]+/", "", $str);
- if (emptyempty($str)) return $default;
- $preg = array(
- "gb2312" => "/^([\xA1-\xF7][\xA0-\xFE])+$/",
- "utf-8" => "/^[\x{4E00}-\x{9FA5}]+$/u",
- );
- if ($default == 'gb2312') {
- $option = 'utf-8';
- } else {
- $option = 'gb2312';
- }
- if (!preg_match($preg[$default], $str)) {
- return $option;
- }
- $str = @iconv($default, $option, $str);
-
- if (emptyempty($str)) {
- return $option;
- }
- return $default;
- }
-
- public static function safeEncoding($string,$outEncoding = 'UTF-8')
- {
- $encoding = "UTF-8";
- for($i = 0; $i strlen ( $string ); $i ++) {
- if (ord ( $string {$i} )
- continue;
- if ((ord ( $string {$i} ) & 224) == 224) {
-
- $char = $string {++ $i};
- if ((ord ( $char ) & 128) == 128) {
-
- $char = $string {++ $i};
- if ((ord ( $char ) & 128) == 128) {
- $encoding = "UTF-8";
- break;
- }
- }
- }
- if ((ord ( $string {$i} ) & 192) == 192) {
-
- $char = $string {++ $i};
- if ((ord ( $char ) & 128) == 128) {
-
- $encoding = "GB2312";
- break;
- }
- }
- }
- if (strtoupper ( $encoding ) == strtoupper ( $outEncoding ))
- return $string;
- else
- return @iconv ( $encoding, $outEncoding, $string );
- }
-
- public static function array_key_values($array =array(), $key='')
- {
- $ret = array();
- foreach((array)$array as $k=>$v){
- $ret[$k] = $v[$key];
- }
- return $ret;
- }
-
- public static function is_writeable($file) {
- if (is_dir($file)){
- $dir = $file;
- if ($fp = @fopen("$dir/test.txt", 'w')) {
- @fclose($fp);
- @unlink("$dir/test.txt");
- $writeable = 1;
- } else {
- $writeable = 0;
- }
- } else {
- if ($fp = @fopen($file, 'a+')) {
- @fclose($fp);
- $writeable = 1;
- } else {
- $writeable = 0;
- }
- }
- return $writeable;
- }
-
- static public function byteFormat( $size, $dec = 2 ) {
- $a = array ( "B" , "KB" , "MB" , "GB" , "TB" , "PB" );
- $pos = 0;
- while ( $size >= 1024 ) {
- $size /= 1024;
- $pos ++;
- }
- return round( $size, $dec ) . " " . $a[$pos];
- }
-
- 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;
- }
-
- static public function downloadImage($url, $filepath, $filename) {
-
- $responseHeaders = array();
-
- $originalfilename = '';
-
- $ext = '';
- $ch = curl_init($url);
-
- curl_setopt($ch, CURLOPT_HEADER, 1);
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
-
- curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
-
- $html = curl_exec($ch);
-
- $httpinfo = curl_getinfo($ch);
- curl_close($ch);
- if ($html !== false) {
-
- $httpArr = explode("\r\n\r\n", $html, 2 + $httpinfo['redirect_count']);
-
- $header = $httpArr[count($httpArr) - 2];
-
- $body = $httpArr[count($httpArr) - 1];
- $header.="\r\n";
-
- preg_match_all('/([a-z0-9-_]+):\s*([^\r\n]+)\r\n/i', $header, $matches);
- if (!emptyempty($matches) && count($matches) == 3 && !emptyempty($matches[1]) && !emptyempty($matches[1])) {
- for ($i = 0; $i count($matches[1]); $i++) {
- if (array_key_exists($i, $matches[2])) {
- $responseHeaders[$matches[1][$i]] = $matches[2][$i];
- }
- }
- }
-
- if (0 '{(?:[^\/\\\\]+)\.(jpg|jpeg|gif|png|bmp)$}i', $url, $matches)) {
- $originalfilename = $matches[0];
- $ext = $matches[1];
- } else {
- if (array_key_exists('Content-Type', $responseHeaders)) {
- if (0 '{image/(\w+)}i', $responseHeaders['Content-Type'], $extmatches)) {
- $ext = $extmatches[1];
- }
- }
- }
-
- if (!emptyempty($ext)) {
-
- if(!is_dir($filepath)){
- mkdir($filepath, 0777, true);
- }
- $filepath .= '/'.$filename.".$ext";
- $local_file = fopen($filepath, 'w');
- if (false !== $local_file) {
- if (false !== fwrite($local_file, $body)) {
- fclose($local_file);
- $sizeinfo = getimagesize($filepath);
- return array('filepath' => realpath($filepath), 'width' => $sizeinfo[0], 'height' => $sizeinfo[1], 'orginalfilename' => $originalfilename, 'filename' => pathinfo($filepath, PATHINFO_BASENAME));
- }
- }
- }
- }
- return false;
- }
-
- 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;
- break;
- }
- }
- }
- }
- return $access;
- }
-
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31