QQ叫紫珊 发表于 2017-3-20 13:17:59

PHP 函数库

  1、关于 URL
<?php/** 返回页面的当前完整路径,包括参数*/function currentUrl() {$url = 'http';if ($_SERVER["HTTPS"] == "on") $url .= "s";$url .= "://";if ($_SERVER["SERVER_PORT"] != "80") {$url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];} else {$url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];}return $url;}/** 返回页面的当前完整路径,不包括参数*/function curUrlNoArgs() {$url = 'http';if ($_SERVER["HTTPS"] == "on") $url .= "s";$url .= "://";$this_page = $_SERVER["REQUEST_URI"];# 不获取?后面的参数if (strpos($this_page, "?") !== false) $this_page = reset(explode("?", $this_page));if ($_SERVER["SERVER_PORT"] != "80") {$url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $this_page;} else {$url .= $_SERVER["SERVER_NAME"] . $this_page;}return $url;}/** $_SERVER['REQUEST_URI'] 的通用解决方案,防止 rewrite 规则可能出现错误*/ function requestURI() {if (isset($_SERVER['REQUEST_URI'])){$uri = $_SERVER['REQUEST_URI'];} else {if (isset($_SERVER['argv'])) {$uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['argv'];} else {$uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'];}}return $uri;}?>
  2、数据入库前格式化数据,防止 SQL 注入
<?php/** 数据入库之前格式化数据(防止 SQL 注入)*/function site_addslashes($string, $force = 0) { !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());if(!MAGIC_QUOTES_GPC || $force) {if(is_array($string)) {foreach($string as $key => $val) {$string[$key] = daddslashes($val, $force);}} else {$string = addslashes($string); }}return $string;}?>
3、PHP 生成 唯一标识符码<?php/** 唯一标识符*/function create_unique() {$data = $_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR'] .time() . rand();return sha1($data);}?>
  4、PHP表单验证类 validator.class.php

  <?php#验证类class validator { /** * 验证邮箱* @param string $string * @param bool $required * @return bool */ public static function isEmail ($string, $required = false) { $isRightFormat = false;if($string == '' && $required ===false) $isRightFormat = true;$exp_match = '/([\w-\.]+)@((\[{1,3}\.{1,3}\.{1,3}\.)|(([\w-]+\.)+))({2,4}|{1,3})(\]?)/';$isRightFormat = preg_match($exp_match, $string) ? true : false;return $isRightFormat;}/** * 验证 URL* @param string $string * @param bool $required * @return bool */ public static function isHttpUrl ($string, $required =false) { $isRightFormat = false;if($string =='' && $required ===false) $isRightFormat = true; $exp_match = '/^http:\/\/+\.+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/';$isRightFormat = preg_math($exp_match, $string) ? true : false;return $isRightFormat;}/** * 验证手机* @param string $string * @param bool $required * @return bool */ public static function isCellPhone ($string, $required = false) { $isRightFormat = false; if($string == '' && $required ===false) $isRightFormat = true; $exp_match = '/^((\(\d{3}\))|(\d{3}\-))?1\d{9}$/';$isRightFormat = preg_match($exp_match, $string) ? true : false;return $isRightFormat;} /** * 验证电话号码* @param string $string * @param bool $required * @return bool */ public static function isPhone ($string, $required = false) { $isRightFormat = false; if($string == "" && $required === false) $isRightFormat = true; $exp_match = '/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/';$isRightFormat = preg_match($exp_match, $string) ? true : false;return $isRightFormat;} /** * 验证邮编* @param string $string* @param bool $required* @return bool*/public static function isZipCode ($string, $required =false) {$isRightFormat = false;if($string == '' && $required ===false) $isRightFormat = true;$exp_match = '/{6}/';$isRightFormat = preg_match($exp_match, $string) ? true : false;return $isRightFormat;}/** * 验证日期(2011-12-30)* @param string $string * @param bool $required * @return bool */ public static function isDateFormat ($string, $required = false) {$isRightFormat = false; if($string == '' && $required === false) $isRightFormat = true; $exp_match = '/^{4}-{1,2}-{1,2}$/';if(preg_match($exp_match, $string)) {$dateArray = explode('-', $string);$isRightFormat = checkdate($dateArray, $dateArray, $dateArray) ? true : false; }return $isRightFormat;} /** * 验证金钱* @param string $string * @param bool $required * @return bool */ public static function isMoney ($string, $required = false) { $isRightFormat = true; if($string == '' && $required === false) $isRightFormat = true;$exp_match = '/^{1,8}[.]{0,1}{0,2}$/'; $isRightFormat = preg_match($exp_match, $string) ? true : false; return $isRightFormat; }} ?>
5、去掉指定的 HTML 标签
  <?php/*** 去掉指定的html标签* @param array $string* @param bool $str * @return string*/function _strip_tags($tagsArr,$str) { foreach ($tagsArr as $tag) {$p[]="/(<(?:\/".$tag."|".$tag.")[^>]*>)/i";}$return_str = preg_replace($p,"",$str);return $return_str;}$str = "<b>您好</b><input type='text' name='' />";_strip_tags(array("b","input"),$str); #去掉 B 标签和 INPUT 标签?>
6、便利文件夹文件
  <?php/* * 遍历文件夹文件,返回数组* @return array*/ function list_files($folder = '', $levels = 100 ) { if(empty($folder)) return false;if(!$levels ) return false;$files = array();if ($dir = @opendir($folder)) {while (($file = readdir($dir))!==false) {if (in_array($file, array('.', '..')))continue;if (is_dir( $folder . '/' . $file )){$files2 = list_files( $folder . '/' . $file, $levels - 1); if( $files2 ) { $files = array_merge($files, $files2 ); } else {$files[] = $folder . '/' . $file . '/';}} else {$files[] = $folder . '/' . $file;}}}@closedir($dir); return $files; }print_r(list_files('phpmyadmin')); # 打印 phpmyadmin 下所有目录和文件?>
7、返回文件类型
  <?php/* * 返回文件类型 * @param string $filename 文件路径* @return array 返回数组,键值分别为:ext、type*/ function getFileType($filename) {$mimes = array( 'jpg|jpeg|jpe' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'bmp' => 'image/bmp', 'tif|tiff' => 'image/tiff', 'ico' => 'image/x-icon', 'asf|asx|wax|wmv|wmx' => 'video/asf', 'avi' => 'video/avi', 'divx' => 'video/divx', 'mov|qt' => 'video/quicktime', 'mpeg|mpg|mpe|mp4' => 'video/mpeg', 'txt|c|cc|h' => 'text/plain', 'rtx' => 'text/richtext', 'css' => 'text/css', 'htm|html' => 'text/html', 'mp3|m4a' => 'audio/mpeg', 'ra|ram' => 'audio/x-realaudio', 'wav' => 'audio/wav', 'ogg' => 'audio/ogg', 'mid|midi' => 'audio/midi', 'wma' => 'audio/wma', 'rtf' => 'application/rtf', 'js' => 'application/javascript', 'pdf' => 'application/pdf', 'doc|docx' => 'application/msword', 'pot|pps|ppt|pptx' => 'application/vnd.ms-powerpoint', 'wri' => 'application/vnd.ms-write', 'xla|xls|xlsx|xlt|xlw' => 'application/vnd.ms-excel', 'mdb' => 'application/vnd.ms-access', 'mpp' => 'application/vnd.ms-project', 'swf' => 'application/x-shockwave-flash', 'class' => 'application/java', 'tar' => 'application/x-tar', 'zip' => 'application/zip', 'gz|gzip' => 'application/x-gzip', 'exe' => 'application/x-msdownload', 'odt' => 'application/vnd.oasis.opendocument.text', 'odp' => 'application/vnd.oasis.opendocument.presentation', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', 'odg' => 'application/vnd.oasis.opendocument.graphics', 'odc' => 'application/vnd.oasis.opendocument.chart', 'odb' => 'application/vnd.oasis.opendocument.database', 'odf' => 'application/vnd.oasis.opendocument.formula');$type = false;$ext = false;foreach ( $mimes as $ext_preg => $mime_match ) {$ext_preg = '!\.(' . $ext_preg . ')$!i';if ( preg_match( $ext_preg, $filename, $ext_matches ) ) {$type = $mime_match;$ext = $ext_matches;break;}}return compact('ext','type'); }print_r(getFileType('123.jpg')); #Array ( => jpg => image/jpeg )?>
8、获取用户真实 IP
  <?php/** * 获得真实IP地址 * @return string */ function realIp() { static $realip = NULL; if ($realip !== NULL) return $realip;if (isset($_SERVER)) {if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);foreach ($arr AS $ip) {$ip = trim($ip);if ($ip != 'unknown') { $realip = $ip; break; } } } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $realip = $_SERVER['HTTP_CLIENT_IP'];} else { if (isset($_SERVER['REMOTE_ADDR'])) { $realip = $_SERVER['REMOTE_ADDR']; } else { $realip = '0.0.0.0'; }}} else {if (getenv('HTTP_X_FORWARDED_FOR')) {$realip = getenv('HTTP_X_FORWARDED_FOR');} elseif (getenv('HTTP_CLIENT_IP')) {$realip = getenv('HTTP_CLIENT_IP');} else {$realip = getenv('REMOTE_ADDR');}}preg_match("/[\d\.]{7,15}/", $realip, $onlineip);$realip = !empty($onlineip) ? $onlineip : '0.0.0.0';return $realip;}?>
9、对要输出的字符串进行反转换
  <?php/** 对要输出的字符串进行反转换*/function Char_rev($msg){ $msg = str_replace('&','&',$msg);$msg = str_replace(' ','',$msg); $msg = str_replace('"','"',$msg); $msg = str_replace("'","'",$msg); $msg = str_replace("<","<",$msg); $msg = str_replace(">",">",$msg); $msg = str_replace("   ","\t",$msg); $msg = str_replace("","\r",$msg); $msg = str_replace("","   ",$msg); $msg = str_replace("<br>","\n",$msg); $msg = str_replace("<br />","\n",$msg); return $msg; } ?>
页: [1]
查看完整版本: PHP 函数库