


A complete list of commonly used PHP tools, a complete list of tools_PHP tutorial
Jul 12, 2016 am 09:03 AMPHP常用工具類大全,工具類大全
<?php /** * 助手類 * @author www.shouce.ren * */ class Helper { /** * 判斷當前服務器系統(tǒng) * @return string */ public static function getOS(){ if(PATH_SEPARATOR == ':'){ return 'Linux'; }else{ return 'Windows'; } } /** * 當前微妙數(shù) * @return number */ public static function microtime_float() { list ( $usec, $sec ) = explode ( " ", microtime () ); return (( float ) $usec + ( float ) $sec); } /** * 切割utf-8格式的字符串(一個漢字或者字符占一個字節(jié)) * * @author zhao jinhan * @version v1.0.0 * */ 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 < 1.0) { 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; } /** * 遍歷文件夾 * @param string $dir * @param boolean $all true表示遞歸遍歷 * @return array */ 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; } /** * 郵件發(fā)送 * @param string $toemail * @param string $subject * @param string $message * @return boolean */ public static function sendMail($toemail = '', $subject = '', $message = '') { $mailer = Yii::createComponent ( 'application.extensions.mailer.EMailer' ); //郵件配置 $mailer->SetLanguage('zh_cn'); $mailer->Host = Yii::app()->params['emailHost']; //發(fā)送郵件服務器 $mailer->Port = Yii::app()->params['emailPort']; //郵件端口 $mailer->Timeout = Yii::app()->params['emailTimeout'];//郵件發(fā)送超時時間 $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; // 發(fā)件人郵箱 $mailer->FromName = Yii::app()->params['emailFormName']; // 發(fā)件人姓名 $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 (); // 發(fā)送郵件 $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編碼的條件了),這個范圍實際上已經(jīng)包含了繁體中文字了 ); if ($default == 'gb2312') { $option = 'utf-8'; } else { $option = 'gb2312'; } if (!preg_match($preg[$default], $str)) { return $option; } $str = @iconv($default, $option, $str); //不能轉(zhuǎn)成 $option, 說明原來的不是 $default if (empty($str)) { return $option; } return $default; } /** * utf-8和gb2312自動轉(zhuǎn)化 * @param unknown $string * @param string $outEncoding * @return unknown|string */ public static function safeEncoding($string,$outEncoding = 'UTF-8') { $encoding = "UTF-8"; for($i = 0; $i < strlen ( $string ); $i ++) { if (ord ( $string {$i} ) < 128) continue; if ((ord ( $string {$i} ) & 224) == 224) { // 第一個字節(jié)判斷通過 $char = $string {++ $i}; if ((ord ( $char ) & 128) == 128) { // 第二個字節(jié)判斷通過 $char = $string {++ $i}; if ((ord ( $char ) & 128) == 128) { $encoding = "UTF-8"; break; } } } if ((ord ( $string {$i} ) & 192) == 192) { // 第一個字節(jié)判斷通過 $char = $string {++ $i}; if ((ord ( $char ) & 128) == 128) { // 第二個字節(jié)判斷通過 $encoding = "GB2312"; break; } } } if (strtoupper ( $encoding ) == strtoupper ( $outEncoding )) return $string; else return @iconv ( $encoding, $outEncoding, $string ); } /** * 返回二維數(shù)組中某個鍵名的所有值 * @param input $array * @param string $key * @return array */ public static function array_key_values($array =array(), $key='') { $ret = array(); foreach((array)$array as $k=>$v){ $ret[$k] = $v[$key]; } return $ret; } /** * 判斷 文件/目錄 是否可寫(取代系統(tǒng)自帶的 is_writeable 函數(shù)) * @param string $file 文件/目錄 * @return boolean */ 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]; } /** * 下拉框,單選按鈕 自動選擇 * * @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) ,此函數(shù)會自動根據(jù)圖片url和http頭信息確定圖片的后綴名 * @param string $filename 要保存的文件名(不含擴展名) * @return mixed 下載成功返回一個描述圖片信息的數(shù)組,下載失敗則返回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內(nèi)容 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //設置抓取跳轉(zhuǎn)(http 301,302)后的頁面 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //設置最多的HTTP重定向的數(shù)量 curl_setopt($ch, CURLOPT_MAXREDIRS, 3); //服務器返回的數(shù)據(jù)(包括http頭信息和內(nèi)容) $html = curl_exec($ch); //獲取此次抓取的相關信息 $httpinfo = curl_getinfo($ch); curl_close($ch); if ($html !== false) { //分離response的header和body,由于服務器可能使用了302跳轉(zhuǎn),所以此處需要將字符串分離為 2+跳轉(zhuǎn)次數(shù) 個子串 $httpArr = explode("\r\n\r\n", $html, 2 + $httpinfo['redirect_count']); //倒數(shù)第二段是服務器最后一次response的http頭 $header = $httpArr[count($httpArr) - 2]; //倒數(shù)第一段是服務器最后一次response的內(nèi)容 $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 < count($matches[1]); $i++) { if (array_key_exists($i, $matches[2])) { $responseHeaders[$matches[1][$i]] = $matches[2][$i]; } } } //獲取圖片后綴名 if (0 < preg_match('{(?:[^\/\\\\]+)\.(jpg|jpeg|gif|png|bmp)$}i', $url, $matches)) { $originalfilename = $matches[0]; $ext = $matches[1]; } else { if (array_key_exists('Content-Type', $responseHeaders)) { if (0 < preg_match('{image/(\w+)}i', $responseHeaders['Content-Type'], $extmatches)) { $ext = $extmatches[1]; } } } //保存文件 if (!empty($ext)) { //如果目錄不存在,則先要創(chuàng)建目錄 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; } /** * 查找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 處理后的 原文或者 經(jīng)過 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'); // 在一個小時內(nèi),$b(abc),否則 $b 為空 */ public static function authcode($string, $operation = 'DECODE', $key = '', $expiry = 3600) { $ckey_length = 4; // 隨機密鑰長度 取值 0-32; // 加入隨機密鑰,可以令密文無任何規(guī)律,即便是原文和密鑰完全相同,加密結果也會每次不同,增大破解難度。 // 取值越大,密文變動規(guī)律越大,密文變化 = 16 的 $ckey_length 次方 // 當此值為 0 時,則不產(chǎn)生隨機密鑰 $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 <= 255; $i ++) { $rndkey [$i] = ord ( $cryptkey [$i % $key_length] ); } for($j = $i = 0; $i < 256; $i ++) { $j = ($j + $box [$i] + $rndkey [$i]) % 256; $tmp = $box [$i]; $box [$i] = $box [$j]; $box [$j] = $tmp; } for($a = $j = $i = 0; $i < $string_length; $i ++) { $a = ($a + 1) % 256; $j = ($j + $box [$a]) % 256; $tmp = $box [$a]; $box [$a] = $box [$j]; $box [$j] = $tmp; $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) ); } if ($operation == 'DECODE') { if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) { return substr ( $result, 26 ); } else { return ''; } } else { return $keyc . str_replace ( '=', '', base64_encode ( $result ) ); } } public static function gbkToUtf8($str){ return iconv("GBK", "UTF-8", $str); } /** * 取得輸入目錄所包含的所有目錄和文件 * 以關聯(lián)數(shù)組形式返回 * author: flynetcn */ static public function deepScanDir($dir) { $fileArr = array(); $dirArr = array(); $dir = rtrim($dir, '//'); if(is_dir($dir)){ $dirHandle = opendir($dir); while(false !== ($fileName = readdir($dirHandle))){ $subFile = $dir . DIRECTORY_SEPARATOR . $fileName; if(is_file($subFile)){ $fileArr[] = $subFile; } elseif (is_dir($subFile) && str_replace('.', '', $fileName)!=''){ $dirArr[] = $subFile; $arr = self::deepScanDir($subFile); $dirArr = array_merge($dirArr, $arr['dir']); $fileArr = array_merge($fileArr, $arr['file']); } } closedir($dirHandle); } return array('dir'=>$dirArr, 'file'=>$fileArr); } /** * 取得輸入目錄所包含的所有文件 * 以數(shù)組形式返回 * 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

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

To prevent session hijacking in PHP, the following measures need to be taken: 1. Use HTTPS to encrypt the transmission and set session.cookie_secure=1 in php.ini; 2. Set the security cookie attributes, including httponly, secure and samesite; 3. Call session_regenerate_id(true) when the user logs in or permissions change to change to change the SessionID; 4. Limit the Session life cycle, reasonably configure gc_maxlifetime and record the user's activity time; 5. Prohibit exposing the SessionID to the URL, and set session.use_only

You can use substr() or mb_substr() to get the first N characters in PHP. The specific steps are as follows: 1. Use substr($string,0,N) to intercept the first N characters, which is suitable for ASCII characters and is simple and efficient; 2. When processing multi-byte characters (such as Chinese), mb_substr($string,0,N,'UTF-8'), and ensure that mbstring extension is enabled; 3. If the string contains HTML or whitespace characters, you should first use strip_tags() to remove the tags and trim() to clean the spaces, and then intercept them to ensure the results are clean.

There are two main ways to get the last N characters of a string in PHP: 1. Use the substr() function to intercept through the negative starting position, which is suitable for single-byte characters; 2. Use the mb_substr() function to support multilingual and UTF-8 encoding to avoid truncating non-English characters; 3. Optionally determine whether the string length is sufficient to handle boundary situations; 4. It is not recommended to use strrev() substr() combination method because it is not safe and inefficient for multi-byte characters.

The urlencode() function is used to encode strings into URL-safe formats, where non-alphanumeric characters (except -, _, and .) are replaced with a percent sign followed by a two-digit hexadecimal number. For example, spaces are converted to signs, exclamation marks are converted to!, and Chinese characters are converted to their UTF-8 encoding form. When using, only the parameter values ??should be encoded, not the entire URL, to avoid damaging the URL structure. For other parts of the URL, such as path segments, the rawurlencode() function should be used, which converts the space to . When processing array parameters, you can use http_build_query() to automatically encode, or manually call urlencode() on each value to ensure safe transfer of data. just

To set and get session variables in PHP, you must first always call session_start() at the top of the script to start the session. 1. When setting session variables, use $_SESSION hyperglobal array to assign values ??to specific keys, such as $_SESSION['username']='john_doe'; it can store strings, numbers, arrays and even objects, but avoid storing too much data to avoid affecting performance. 2. When obtaining session variables, you need to call session_start() first, and then access the $_SESSION array through the key, such as echo$_SESSION['username']; it is recommended to use isset() to check whether the variable exists to avoid errors

Key methods to prevent SQL injection in PHP include: 1. Use preprocessing statements (such as PDO or MySQLi) to separate SQL code and data; 2. Turn off simulated preprocessing mode to ensure true preprocessing; 3. Filter and verify user input, such as using is_numeric() and filter_var(); 4. Avoid directly splicing SQL strings and use parameter binding instead; 5. Turn off error display in the production environment and record error logs. These measures comprehensively prevent the risk of SQL injection from mechanisms and details.
