".$content."
".$script."摘要:有可能我們用的一些框架已經幫助我們封裝好了這些方法,我們平常經常用到獲取地址創建目錄之類的方法,但是我還是想摘出來放在下面,可能大家已經都已經熟悉了。
有可能我們用的一些框架已經幫助我們封裝好了這些方法,我們平常經常用到獲取ip地址、創建目錄之類的方法,但是我還是想摘出來放在下面,可能大家已經都已經熟悉了。
* @copyright Copyright (c) 2007-2013 bagesoft. All rights reserved. * @link http://www.bagecms.com * @package BageCMS.Tools * @license http://www.bagecms.com/license * @version v3.1.0 */ class XUtils { /** * 友好顯示var_dump */ static public function dump( $var, $echo = true, $label = null, $strict = true ) { $label = ( $label === null ) ? "" : rtrim( $label ) . " "; if ( ! $strict ) { if ( ini_get( "html_errors" ) ) { $output = print_r( $var, true ); $output = "" . $label . htmlspecialchars( $output, ENT_QUOTES ) . ""; } else { $output = $label . print_r( $var, true ); } } else { ob_start(); var_dump( $var ); $output = ob_get_clean(); if ( ! extension_loaded( "xdebug" ) ) { $output = preg_replace( "/]=> (s+)/m", "] => ", $output ); $output = "" . $label . htmlspecialchars( $output, ENT_QUOTES ) . ""; } } if ( $echo ) { echo $output; return null; } else return $output; } /** * 獲取客戶端IP地址 */ static public function getClientIP() { static $ip = NULL; if ( $ip !== NULL ) return $ip; if ( isset( $_SERVER["HTTP_X_FORWARDED_FOR"] ) ) { $arr = explode( ",", $_SERVER["HTTP_X_FORWARDED_FOR"] ); $pos = array_search( "unknown", $arr ); if ( false !== $pos ) unset( $arr[$pos] ); $ip = trim( $arr[0] ); } elseif ( isset( $_SERVER["HTTP_CLIENT_IP"] ) ) { $ip = $_SERVER["HTTP_CLIENT_IP"]; } elseif ( isset( $_SERVER["REMOTE_ADDR"] ) ) { $ip = $_SERVER["REMOTE_ADDR"]; } // IP地址合法驗證 $ip = ( false !== ip2long( $ip ) ) ? $ip : "0.0.0.0"; return $ip; } /** * 循環創建目錄 */ static public function mkdir( $dir, $mode = 0777 ) { if ( is_dir( $dir ) || @mkdir( $dir, $mode ) ) return true; if ( ! mk_dir( dirname( $dir ), $mode ) ) return false; return @mkdir( $dir, $mode ); } /** * 格式化單位 */ 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]; } /** * 下拉框,單選按鈕 自動選擇 * * @param $string 輸入字符 * @param $param 條件 * @param $type 類型 * selected checked * @return string */ static public function selected( $string, $param = 1, $type = "select" ) { if ( is_array( $param ) ) { $true = in_array( $string, $param ); }elseif ( $string == $param ) { $true = true; } if ( $true ) $return = $type == "select" ? "selected="selected"" : "checked="checked""; echo $return; } /** * 獲得來源類型 post get * * @return unknown */ static public function method() { return strtoupper( isset( $_SERVER["REQUEST_METHOD"] ) ? $_SERVER["REQUEST_METHOD"] : "GET" ); } /** * 提示信息 */ static public function message( $action = "success", $content = "", $redirect = "", $timeout = 3 ) { $url = $redirect; switch ( $action ) { case "success": $vars = array("titler"=>"操作完成", "class"=>"success","status"=>"?"); break; case "error": $vars = array("titler"=>"操作未完成", "class"=>"error","status"=>"?"); break; case "errorBack": $vars = array("titler"=>"操作未完成", "class"=>"error","status"=>"?"); break; case "redirect": header( "Location:$url" ); break; case "script": exit( "" ); break; } if($action !="errorBack") $script = "系統自動跳轉在 ".$timeout." 秒鐘后,如果不想等待 > 點擊這里跳轉"; else $script = "[點這里返回上一頁]"; $body = ""; exit ( $body ); } /** * 查詢字符生成 */ static public function buildCondition( array $getArray, array $keys = array() ) { if ( $getArray ) { foreach ( $getArray as $key => $value ) { if ( in_array( $key, $keys ) && $value ) { $arr[$key] = CHtml::encode(strip_tags($value)); } } return $arr; } } /** * base64_encode */ static function b64encode( $string ) { $data = base64_encode( $string ); $data = str_replace( array ( "+" , "/" , "=" ), array ( "-" , "_" , "" ), $data ); return $data; } /** * base64_decode */ static function b64decode( $string ) { $data = str_replace( array ( "-" , "_" ), array ( "+" , "/" ), $string ); $mod4 = strlen( $data ) % 4; if ( $mod4 ) { $data .= substr( "====", $mod4 ); } return base64_decode( $data ); } /** * 驗證郵箱 */ public static function email( $str ) { if ( empty( $str ) ) return true; $chars = "/^([a-z0-9+_]|-|.)+@(([a-z0-9_]|-)+.)+[a-z]{2,6}$/i"; if ( strpos( $str, "@" ) !== false && strpos( $str, "." ) !== false ) { if ( preg_match( $chars, $str ) ) { return true; } else { return false; } } else { return false; } } /** * 驗證手機號碼 */ public static function mobile( $str ) { if ( empty( $str ) ) { return true; } return preg_match( "#^13[d]{9}$|14^[0-9]d{8}|^15[0-9]d{8}$|^18[0-9]d{8}$#", $str ); } /** * 驗證固定電話 */ public static function tel( $str ) { if ( empty( $str ) ) { return true; } return preg_match( "/^(((d{2,3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}(-d{1,4})?$/", trim( $str ) ); } /** * 驗證qq號碼 */ public static function qq( $str ) { if ( empty( $str ) ) { return true; } return preg_match( "/^[1-9]d{4,12}$/", trim( $str ) ); } /** * 驗證郵政編碼 */ public static function zipCode( $str ) { if ( empty( $str ) ) { return true; } return preg_match( "/^[1-9]d{5}$/", trim( $str ) ); } /** * 驗證ip */ public static function ip( $str ) { if ( empty( $str ) ) return true; if ( ! preg_match( "#^d{1,3}.d{1,3}.d{1,3}.d{1,3}$#", $str ) ) { return false; } $ip_array = explode( ".", $str ); //真實的ip地址每個數字不能大于255(0-255) return ( $ip_array[0] <= 255 && $ip_array[1] <= 255 && $ip_array[2] <= 255 && $ip_array[3] <= 255 ) ? true : false; } /** * 驗證身份證(中國) */ public static function idCard( $str ) { $str = trim( $str ); if ( empty( $str ) ) return true; if ( preg_match( "/^([0-9]{15}|[0-9]{17}[0-9a-z])$/i", $str ) ) return true; else return false; } /** * 驗證網址 */ public static function url( $str ) { if ( empty( $str ) ) return true; return preg_match( "#(http|https|ftp|ftps)://([w-]+.)+[w-]+(/[w-./?%&=]*)?#i", $str ) ? true : false; } /** * 根據ip獲取地理位置 * @param $ip * return :ip,beginip,endip,country,area */ public static function getlocation( $ip = "" ) { $ip = new XIp(); $ipArr = $ip->getlocation( $ip ); return $ipArr; } /** * 中文轉換為拼音 */ public static function pinyin( $str ) { $ip = new XPinyin(); return $ip->output( $str ); } /** * 拆分sql * * @param $sql */ public static function splitsql( $sql ) { $sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=1 DEFAULT CHARSET=" . Yii::app()->db->charset, $sql); $sql = str_replace(" ", " ", $sql); $ret = array (); $num = 0; $queriesarray = explode("; ", trim($sql)); unset($sql); foreach ($queriesarray as $query) { $ret[$num] = ""; $queries = explode(" ", trim($query)); $queries = array_filter($queries); foreach ($queries as $query) { $str1 = substr($query, 0, 1); if ($str1 != "#" && $str1 != "-") $ret[$num] .= $query; } $num ++; } return ($ret); } /** * 字符截取 * * @param $string * @param $length * @param $dot */ public static function cutstr( $string, $length, $dot = "...", $charset = "utf-8" ) { if ( strlen( $string ) <= $length ) return $string; $pre = chr( 1 ); $end = chr( 1 ); $string = str_replace( array ( "&" , """ , "<" , ">" ), array ( $pre . "&" . $end , $pre . """ . $end , $pre . "<" . $end , $pre . ">" . $end ), $string ); $strcut = ""; if ( strtolower( $charset ) == "utf-8" ) { $n = $tn = $noc = 0; while ( $n < strlen( $string ) ) { $t = ord( $string[$n] ); if ( $t == 9 || $t == 10 || ( 32 <= $t && $t <= 126 ) ) { $tn = 1; $n ++; $noc ++; } elseif ( 194 <= $t && $t <= 223 ) { $tn = 2; $n += 2; $noc += 2; } elseif ( 224 <= $t && $t <= 239 ) { $tn = 3; $n += 3; $noc += 2; } elseif ( 240 <= $t && $t <= 247 ) { $tn = 4; $n += 4; $noc += 2; } elseif ( 248 <= $t && $t <= 251 ) { $tn = 5; $n += 5; $noc += 2; } elseif ( $t == 252 || $t == 253 ) { $tn = 6; $n += 6; $noc += 2; } else { $n ++; } if ( $noc >= $length ) { break; } } if ( $noc > $length ) { $n -= $tn; } $strcut = substr( $string, 0, $n ); } else { for ( $i = 0; $i < $length; $i ++ ) { $strcut .= ord( $string[$i] ) > 127 ? $string[$i] . $string[++ $i] : $string[$i]; } } $strcut = str_replace( array ( $pre . "&" . $end , $pre . """ . $end , $pre . "<" . $end , $pre . ">" . $end ), array ( "&" , """ , "<" , ">" ), $strcut ); $pos = strrpos( $strcut, chr( 1 ) ); if ( $pos !== false ) { $strcut = substr( $strcut, 0, $pos ); } return $strcut . $dot; } /** * 描述格式化 * @param $subject */ public static function clearCutstr ($subject, $length = 0, $dot = "...", $charset = "utf-8") { if ($length) { return XUtils::cutstr(strip_tags(str_replace(array (" " ), "", $subject)), $length, $dot, $charset); } else { return strip_tags(str_replace(array (" " ), "", $subject)); } } /** * 檢測是否為英文或英文數字的組合 * * @return unknown */ public static function isEnglist( $param ) { if ( ! eregi( "^[A-Z0-9]{1,26}$", $param ) ) { return false; } else { return true; } } /** * 將自動判斷網址是否加http:// * * @param $http * @return string */ public static function convertHttp( $url ) { if ( $url == "http://" || $url == "" ) return ""; if ( substr( $url, 0, 7 ) != "http://" && substr( $url, 0, 8 ) != "https://" ) $str = "http://" . $url; else $str = $url; return $str; } /* 標題樣式格式化 */ public static function titleStyle( $style ) { $text = ""; if ( $style["bold"] == "Y" ) { $text .="font-weight:bold;"; $serialize["bold"] = "Y"; } if ( $style["underline"] == "Y" ) { $text .="text-decoration:underline;"; $serialize["underline"] = "Y"; } if ( !empty( $style["color"] ) ) { $text .="color:#".$style["color"].";"; $serialize["color"] = $style["color"]; } return array( "text" => $text, "serialize"=>empty( $serialize )? "": serialize( $serialize ) ); } // 自動轉換字符集 支持數組轉換 static public function autoCharset ($string, $from = "gbk", $to = "utf-8") { $from = strtoupper($from) == "UTF8" ? "utf-8" : $from; $to = strtoupper($to) == "UTF8" ? "utf-8" : $to; if (strtoupper($from) === strtoupper($to) || empty($string) || (is_scalar($string) && ! is_string($string))) { //如果編碼相同或者非字符串標量則不轉換 return $string; } if (is_string($string)) { if (function_exists("mb_convert_encoding")) { return mb_convert_encoding($string, $to, $from); } elseif (function_exists("iconv")) { return iconv($from, $to, $string); } else { return $string; } } elseif (is_array($string)) { foreach ($string as $key => $val) { $_key = self::autoCharset($key, $from, $to); $string[$_key] = self::autoCharset($val, $from, $to); if ($key != $_key) unset($string[$key]); } return $string; } else { return $string; } } /* 標題樣式恢復 */ public static function titleStyleRestore( $serialize, $scope = "bold" ) { $unserialize = unserialize( $serialize ); if ( $unserialize["bold"] =="Y" && $scope == "bold" ) return "Y"; if ( $unserialize["underline"] =="Y" && $scope == "underline" ) return "Y"; if ( $unserialize["color"] && $scope == "color" ) return $unserialize["color"]; } /** * 列出文件夾列表 * * @param $dirname * @return unknown */ public static function getDir( $dirname ) { $files = array(); if ( is_dir( $dirname ) ) { $fileHander = opendir( $dirname ); while ( ( $file = readdir( $fileHander ) ) !== false ) { $filepath = $dirname . "/" . $file; if ( strcmp( $file, "." ) == 0 || strcmp( $file, ".." ) == 0 || is_file( $filepath ) ) { continue; } $files[] = self::autoCharset( $file, "GBK", "UTF8" ); } closedir( $fileHander ); } else { $files = false; } return $files; } /** * 列出文件列表 * * @param $dirname * @return unknown */ public static function getFile( $dirname ) { $files = array(); if ( is_dir( $dirname ) ) { $fileHander = opendir( $dirname ); while ( ( $file = readdir( $fileHander ) ) !== false ) { $filepath = $dirname . "/" . $file; if ( strcmp( $file, "." ) == 0 || strcmp( $file, ".." ) == 0 || is_dir( $filepath ) ) { continue; } $files[] = self::autoCharset( $file, "GBK", "UTF8" );; } closedir( $fileHander ); } else { $files = false; } return $files; } /** * [格式化圖片列表數據] * * @return [type] [description] */ public static function imageListSerialize( $data ) { foreach ( (array)$data["file"] as $key => $row ) { if ( $row ) { $var[$key]["fileId"] = $data["fileId"][$key]; $var[$key]["file"] = $row; } } return array( "data"=>$var, "dataSerialize"=>empty( $var )? "": serialize( $var ) ); } /** * 反引用一個引用字符串 * @param $string * @return string */ static function stripslashes($string) { if(is_array($string)) { foreach($string as $key => $val) { $string[$key] = self::stripslashes($val); } } else { $string = stripslashes($string); } return $string; } /** * 引用字符串 * @param $string * @param $force * @return string */ static function addslashes($string, $force = 1) { if(is_array($string)) { foreach($string as $key => $val) { $string[$key] = self::addslashes($val, $force); } } else { $string = addslashes($string); } return $string; } /** * 格式化內容 */ static function formatHtml($content, $options = ""){ $purifier = new CHtmlPurifier(); if($options != false) $purifier->options = $options; return $purifier->purify($content); } } ?>".$vars["titler"]." ".$vars["status"]."".$script."
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/21908.html
摘要:使用方法如下截取字符串長度我們經常會遇到需要截取字符串含中文漢字長度的情況,比如標題顯示不能超過多少字符,超出的長度用表示,以下函數可以滿足你的需求。,下載創建一個目錄樹 1、PHP加密解密 PHP加密和解密函數可以用來加密一些有用的字符串存放在數據庫里,并且通過可逆解密字符串,該函數使用了base64和MD5加密和解密。 function encryptDecrypt($key, $...
摘要:熟悉編程,對于網絡模型有一定的認知,熟悉多路復用技術。對主從延遲能有效解決。能夠支持對于千萬級流量網站的正常開發維護工作。結束語技術無止境,未來有更多可能。 本文旨在給要學習 PHP 的新手一個大概的認知輪廓,在心里有個學習的結構,有的放矢,避免走太多彎路。大神請忽略。 入門階段 預備知識 1、掌握基本HTML、JS、CSS語法;熟悉 Bootstrap。 參考: https:/...
閱讀 1846·2021-08-19 11:12
閱讀 1423·2021-07-25 21:37
閱讀 986·2019-08-30 14:07
閱讀 1265·2019-08-30 13:12
閱讀 649·2019-08-30 11:00
閱讀 3527·2019-08-29 16:28
閱讀 987·2019-08-29 15:33
閱讀 2965·2019-08-26 13:40