Laravel--一般的なグローバル関数

30945 ワード

よく使われるグローバル関数はapp/include/globalに配置される.func.php
//      
function myLog($filename, $content) {
    $handle = fopen($filename, 'a');
    fwrite($handle, date('[Y-m-d H:i:s] ') . $content . "
"
); fclose($handle); } function weiXinToScreenLog($content,$file='') { if(!$file){ $file = PUBLIC_PATH.'/log/'.date('Y').'/'.date('md').'/log.log'; } $dir = dirname($file); if (!file_exists($dir)) { mkdir($dir, 0777, true); } myLog($file, $content); } /** * :unicode2utf8_2("\ue056"),\ue056 http://code.iamcal.com/php/emoji/ Softbank */ function unicode2utf8_2($str){ // unicode , emoji $str = '{"result_str":"'.$str.'"}'; // json $strarray = json_decode($str,true); //json , JSON \uXXXX Unicode return $strarray['result_str']; } /** * 、 、 '%+-()' * @access public * @param string $str * @return string $str */ function make_semiangle($str) { $arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E', 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J', 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O', 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T', 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y', 'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i', 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n', 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x', 'y' => 'y', 'z' => 'z', '(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[', '】' => ']', '〖' => '[', '〗' => ']', '“' => '"', '”' => '"', '‘' => '\'', '’' => '\'', '{' => '{', '}' => '}', '《' => ', '》' => '>', '%' => '%', '+' => '+', '—' => '-', '-' => '-', ':' => ':', '。' => '.', '、' => '\\', ',' => ',', ';' => ';', '?' => '?', '!' => '!', '……' => '^', '‖' => '|', '|' => '|', '〃' => '"', '·'=>'`','~'=>'~', ' ' => ' ','$'=>'$','@'=>'@','#'=>'#','^'=>'^','&'=>'&','*'=>'*', '"'=>'"'); return strtr($str, $arr); } // function mkdirs($path, $mode = 0777) { if(!file_exists($path)) { mkdirs(dirname($path), $mode); mkdir($path,$mode); } } // cookie function set_cookie($var, $value = '', $time = 0) { $time = $time > 0 ? $time : ($value == '' ? PHP_TIME - 3600 : 0); $s = $_SERVER['SERVER_PORT'] == '443' ? 1 : 0; $var = COOKIE_PRE.$var; $_COOKIE[$var] = $value; if(is_array($value)){ foreach($value as $k=>$v){ $v = Crypt::encrypt($v); setcookie($var.'['.$k.']', $v, $time, COOKIE_PATH, COOKIE_DOMAIN, $s); } }else{ $value = Crypt::encrypt($value); setcookie($var, $value, $time, COOKIE_PATH, COOKIE_DOMAIN, $s); } } // cookie function get_cookie($var) { $var = COOKIE_PRE.$var; $r = isset($_COOKIE[$var]) ? $_COOKIE[$var] : 0; if(!($r===0)){ $r = Crypt::decrypt($r); } return $r; } // function get_filetype($filename) { $extend = explode("." , $filename); return strtolower($extend[count($extend) - 1]); } function resizeImage($im, $dest, $maxwidth, $maxheight) { $img = getimagesize($im); switch ($img[2]) { case 1: $im = @imagecreatefromgif($im); break; case 2: $im = @imagecreatefromjpeg($im); break; case 3: $im = @imagecreatefrompng($im); break; } $pic_width = imagesx($im); $pic_height = imagesy($im); $resizewidth_tag = false; $resizeheight_tag = false; if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) { if ($maxwidth && $pic_width > $maxwidth) { $widthratio = $maxwidth / $pic_width; $resizewidth_tag = true; } if ($maxheight && $pic_height > $maxheight) { $heightratio = $maxheight / $pic_height; $resizeheight_tag = true; } if ($resizewidth_tag && $resizeheight_tag) { if ($widthratio < $heightratio) $ratio = $widthratio; else $ratio = $heightratio; } if ($resizewidth_tag && !$resizeheight_tag) $ratio = $widthratio; if ($resizeheight_tag && !$resizewidth_tag) $ratio = $heightratio; $newwidth = $pic_width * $ratio; $newheight = $pic_height * $ratio; if (function_exists("imagecopyresampled")) { $newim = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height); } else { $newim = imagecreate($newwidth, $newheight); imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height); } imagejpeg($newim, $dest); imagedestroy($newim); } else { imagejpeg($im, $dest); } } // routeName function getRouteName($i=1){ $url = $_SERVER['REQUEST_URI']; $url = str_replace(strstr($url,'?'),'',$url); $aItems = explode('/',$url); $class = isset($aItems[$i])?$aItems[$i]:''; return $class; } function safe_replace($string) { $string = str_replace('%20','',$string); $string = str_replace('%27','',$string); $string = str_replace('*','',$string); $string = str_replace('"','"',$string); $string = str_replace("'",'',$string); $string = str_replace("\"",'',$string); $string = str_replace('//','',$string); $string = str_replace(';','',$string); $string = str_replace(','<',$string); $string = str_replace('>','>',$string); $string = str_replace('(','',$string); $string = str_replace(')','',$string); $string = str_replace("{",'',$string); $string = str_replace('}','',$string); return $string; } /** *curl */ function httpsRequest($url, $postData="") { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if($postData){ curl_setopt($curl, CURLOPT_POST, 1); //post curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); // post } ob_start(); $data = curl_exec($curl); $data= ob_get_clean(); if (curl_errno($curl)) { return 'ERROR '.curl_error($curl); } curl_close($curl); return json_decode($data, true); }