本篇文章給大家分享的是有關(guān)怎么對PHP的功能函數(shù)進(jìn)行自定義,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
做網(wǎng)站、成都網(wǎng)站制作介紹好的網(wǎng)站是理念、設(shè)計和技術(shù)的結(jié)合。創(chuàng)新互聯(lián)擁有的網(wǎng)站設(shè)計理念、多方位的設(shè)計風(fēng)格、經(jīng)驗豐富的設(shè)計團隊。提供PC端+手機端網(wǎng)站建設(shè),用營銷思維進(jìn)行網(wǎng)站設(shè)計、采用先進(jìn)技術(shù)開源代碼、注重用戶體驗與SEO基礎(chǔ),將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺化效果。注意:部分函數(shù)可能不夠完善,由此造成的漏洞風(fēng)險自擔(dān)~
提交過濾
function filter($text) { //完全過濾注釋 $text = preg_replace('/<!--?.*-->/', '', $text); //完全過濾js $text = preg_replace('/<script?.*\/script>/', '', $text); //過濾危險的屬性,如:過濾on事件lang js while (preg_match('/(<[^><]+)( lang|action|background|codebase|dynsrc|lowsrc)[^><]+/i', $text, $mat)) { $text = str_replace($mat[0], $mat[1], $text); } while (preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i', $text, $mat)) { $text = str_replace($mat[0], $mat[1] . $mat[3], $text); } //過濾多余html $text = preg_replace('/<\/?(html|head|meta|link|base|basefont|body|bgsound|script|form|iframe|frame|frameset|applet|id|ilayer|layer|name|script|xml)[^><]*>/i', '', $text); //反轉(zhuǎn)換 $text = str_replace('[', '<', $text); $text = str_replace(']', '>', $text); $text = str_replace('|', '"', $text); return $text; }
提交過濾2
function stripslashes_array(&$array) { while (list($key, $var) = each($array)) { if ($key != 'argc' && $key != 'argv' && (strtoupper($key) != $key || '' . intval($key) == "$key")) { if (is_string($var)) { $array[$key] = stripslashes($var); } if (is_array($var)) { $array[$key] = stripslashes_array($var); } } } return $array; }
字符串截取(這個是emlog里提出來的,其實也就是substr的增強版)
function subString($strings, $start, $length) { if (function_exists('mb_substr') && function_exists('mb_strlen')) { $sub_str = mb_substr($strings, $start, $length, 'utf8'); return mb_strlen($sub_str, 'utf8') < mb_strlen($strings, 'utf8') ? $sub_str . '...' : $sub_str; } $str = substr($strings, $start, $length); $char = 0; for ($i = 0; $i < strlen($str); $i++) { if (ord($str[$i]) >= 128) $char++; } $str2 = substr($strings, $start, $length + 1); $str3 = substr($strings, $start, $length + 2); if ($char % 3 == 1) { if ($length <= strlen($strings)) { $str3 = $str3 .= '...'; } return $str3; } if ($char % 3 == 2) { if ($length <= strlen($strings)) { $str2 = $str2 .= '...'; } return $str2; } if ($char % 3 == 0) { if ($length <= strlen($strings)) { $str = $str .= '...'; } return $str; } }
IP地址匿名化(最后一位替換為星號)
function AnonymousIP() { $ip = GetIP(); if ($ip == "Unknown") { $removed_ip = "未知用戶"; } if (strpos($ip, ":")) { $removed_ip = "IPv6用戶"; } else { $reg1 = '/((?:\d+\.){3})\d+/'; $reg2 = '~(\d+)\.(\d+)\.(\d+)\.(\d+)~'; $removed_ip = preg_replace($reg1, "\\1*", $ip); } return $removed_ip; }
獲取客戶端IP地址
function GetIP() { if (@$_SERVER["HTTP_X_FORWARDED_FOR"]) $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; else if (@$_SERVER["HTTP_CLIENT_IP"]) $ip = $_SERVER["HTTP_CLIENT_IP"]; else if (@$_SERVER["REMOTE_ADDR"]) $ip = $_SERVER["REMOTE_ADDR"]; else if (@getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR"); else if (@getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP"); else if (@getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR"); else $ip = "Unknown"; return $ip; }
字符串加密(支持中文)
function dencrypt($string, $isEncrypt = true, $key = "youdian") { if (!isset($string{0}) || !isset($key{0})) { return false; } $dynKey = $isEncrypt ? hash('sha1', microtime(true)) : substr($string, 0, 40); $fixedKey = hash('sha1', $key); $dynKeyPart1 = substr($dynKey, 0, 20); $dynKeyPart2 = substr($dynKey, 20); $fixedKeyPart1 = substr($fixedKey, 0, 20); $fixedKeyPart2 = substr($fixedKey, 20); $key = hash('sha1', $dynKeyPart1 . $fixedKeyPart1 . $dynKeyPart2 . $fixedKeyPart2); $string = $isEncrypt ? $fixedKeyPart1 . $string . $dynKeyPart2 : (isset($string{339}) ? gzuncompress(base64_decode(substr($string, 40))) : base64_decode(substr($string, 40))); $n = 0; $result = ''; $len = strlen($string); for ($n = 0; $n < $len; $n++) { $result .= chr(ord($string{$n}) ^ ord($key{$n % 40})); } return $isEncrypt ? $dynKey . str_replace('=', '', base64_encode($n > 299 ? gzcompress($result) : $result)) : substr($result, 20, -20); }
檢查字符串是否包含中文
function CheckChinese($string) { if (preg_match("/[\x7f-\xff]/", $string)) { return true; } else { return false; } }
隱藏文件真實路徑
function FileHeaderJump($file) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; }
生成隨機字符串,自定義長度
function CreateRandomString($length) { $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $password = ""; for ($i = 0; $i < $length; $i++) { $password .= $chars[mt_rand(0, strlen($chars) - 1)]; } return $password; }
以上就是怎么對PHP的功能函數(shù)進(jìn)行自定義,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章題目:怎么對PHP的功能函數(shù)進(jìn)行自定義-創(chuàng)新互聯(lián)
地址分享:http://aaarwkj.com/article14/gccde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、ChatGPT、網(wǎng)站建設(shè)、建站公司、品牌網(wǎng)站設(shè)計、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容