PHP编程中常用自定义函数
来源:互联网 发布:sketch有windows版吗 编辑:程序博客网 时间:2024/06/01 10:13
PHP编程中常用自定义函数
php去掉字符串中的空格
我们平时用的trim($str)只能去掉两头的空格
如果我们需要去掉中间的空格,可以这样:
str_replace(chr(32),"",$str);
如:
$str="a b c";
echo trim($str)."/n"; // output 'a b c'
echo str_replace(chr(32),"",$str); //output 'abc'
echo trim($str)."/n"; // output 'a b c'
echo str_replace(chr(32),"",$str); //output 'abc'
生成时间 function maketime($date) { if($date){ list($year,$month,$day) = explode("-",$date); return mktime(0,0,0,$month,$day,$year); } return ""; } 生成目录: function Directory($dir){ // force directory structure return is_dir($dir) or (Directory(dirname($dir)) and mkdir($dir, 0777)); } 后退 function history_back($go="") { if($go){ echo "<script language=/"javascript/">history.go(".$go."); </script>": } else{ echo "<script language=/"javascript/">history.go(-1);</script>": } } 消息提示 function message($C_alert,$I_goback='') { if(!empty($I_goback)) { echo "<script>alert('$C_alert');window.location.href='$I_goback';</script>"; } else { echo "<script>alert('$C_alert');</script>"; } } /** * 截取中文部分字符串 * * 截取指定字符串指定长度的函数,该函数可自动判定中英文,不会出现乱码 * * @access public * @param string $str 要处理的字符串 * @param int $strlen 要截取的长度默认为10 * @param string $other 是否要加上省略号,默认会加上 * @return string */ function showtitle($str,$strlen=10,$other=true) { $j = 0; for($i=0;$i<$strlen;$i++) if(ord(substr($str,$i,1))>0xa0) $j++; if($j%2!=0) $strlen++; $rstr=substr($str,0,$strlen); if (strlen($str)>$strlen && $other) return $rstr; } //////////// function dhtmlspecialchars($string) { if(is_array($string)) { foreach($string as $key => $val) { $string[$key] = dhtmlspecialchars($val); } } else { $string = str_replace('&', '&', $string); $string = str_replace('"', '"', $string); $string = str_replace('<', '<', $string); $string = str_replace('>', '>', $string); $string = preg_replace('/&(#/d;)/', '&/1', $string); } return $string; } ///////// function daddslashes($string, $force = 0) { if(!$GLOBALS['magic_quotes_gpc'] || $force) { if(is_array($string)) { foreach($string as $key => $val) { $string[$key] = daddslashes($val, $force); } } else { $string = addslashes($string); } } return $string; } /////////. function random($length) { $hash = ''; $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; $max = strlen($chars) - 1; mt_srand((double)microtime() * 1000000); for($i = 0; $i < $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } ////// function wordscut($string, $length ,$sss=0) { if(strlen($string) > $length) { if($sss){ $length=$length - 3; $addstr=' ...'; } for($i = 0; $i < $length; $i++) { if(ord($string[$i]) > 127) { $wordscut .= $string[$i].$string[$i + 1]; $i++; } else { $wordscut .= $string[$i]; } } return $wordscut.$addstr; } return $string; } ///////// 百分比 function percent($a,$b,$float="") { if($b){ return (round($a/$b,$float+2)*100)."%"; } return ""; } /* 获取当前目录下的所有文件的个数 */ function countDirFiles($dir="") { $files = 0; if(!$dir){ $dir="."; } if(is_dir($dir)){ if($handle = opendir($dir)) { while(false !== ($file = readdir($handle))) { if(is_file($file)) { $files++; } } closedir($handle); } } return $files; } echo countDirFiles(); /* 获取当前目录下的所有文件 */ function listFiles($dir="") { $files = array(); if(!$dir){ $dir="."; } if(is_dir($dir)){ if($handle = opendir($dir)) { while(false !== ($file = readdir($handle))) { if(is_file($file)) { $files[]=$file; } } closedir($handle); } } return $files; } print_r(listFiles(get_dir().'www/')); /* 获取当前目录相对于根目录的相对路径 */ function get_dir() { $biasNum = substr_count($_SERVER['REQUEST_URI'], '/'); //用'/'分割当前路径字符串,并计算分割后的字符串数量 $relativePath = '../'; //初始化变量$relativePath为'./' for ($i = 0; $i < ($biasNum - 1); $i ++) { //循环添加'../' $relativePath .= '../'; } return $relativePath; } |
- PHP编程中常用自定义函数
- php常用自定义函数
- PHP 常用自定义函数
- 【PHP】开发中常用自定义小功能函数
- PHP项目开发中最常用的自定义函数整理
- 个人常用自定义php函数
- 【php】php中常用函数
- php的一些常用自定义函数
- mysql中自定义函数编程
- mysql中自定义函数编程
- PHP中常用MySQL函数
- php中session常用函数
- php中session常用函数
- php中常用函数技巧
- php中常用数组函数
- php中常用的函数
- PHP中正则常用函数
- php中mysql常用函数
- 日泽IT笔记系列1-Beginning C# Objects 从概念到代码-1
- 日泽IT笔记系列1-Beginning C# Objects 从概念到代码-2
- JAVA中的包和环境变量的设置问题
- Linux上安装VMware 6.5 tools
- 利用内存映射文件在两个进程间共享数据
- PHP编程中常用自定义函数
- SVN整合Apache的问题
- OA选择首先要清晰概念
- 读博申请的新变化:香港理工-->上海交大or哈工大
- PHP编程面试题
- 删除自定义的名称
- PHP编程常用技巧四则
- 怎样才能成为PHP高手
- java Native Method初涉