您好,欢迎访问一九零五行业门户网

超实用PHP函数总结整理,实用php函数总结_PHP教程

超实用php函数总结整理,实用php函数总结1、php加密解密php加密和解密函数可以用来加密一些有用的字符串存放在数据库里,并且通过可逆解密字符串,该函数使用了base64和md5加密和解密。
1 function encryptdecrypt($key, $string, $decrypt){ 2 3 if($decrypt){ 4 5 $decrypted = rtrim(mcrypt_decrypt(mcrypt_rijndael_256, md5($key), base64_decode 6 ($string), mcrypt_mode_cbc, md5(md5($key))), 12); 7 8 return $decrypted; 9 10 }else{ 11 12 $encrypted = base64_encode(mcrypt_encrypt(mcrypt_rijndael_256, md5($key),13 $string, mcrypt_mode_cbc, md5(md5($key)))); 14 15 return $encrypted; 16 17 } 18 19 }
使用方法如下:
1 //以下是将字符串“helloweba欢迎您”分别加密和解密 2 3 //加密: 4 5 echo encryptdecrypt('password', 'helloweba欢迎您',0); 6 7 //解密: 8 9 echo encryptdecrypt('password', 'z0jax4qmwcf+db5tnbp/xwdum84snrsxvvpxuaca4bk=',1);
2、php生成随机字符串 当我们需要生成一个随机名字,临时密码等字符串时可以用到下面的函数:
1 function generaterandomstring($length = 10) { 2 3 $characters = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; 4 5 $randomstring = ''; 6 7 for ($i = 0; $i '','{/b}' => '','{br}' => ''); 15 16 17 18 echo stringparser($string,$replace_array);
6、php列出目录下的文件名如果你想列出目录下的所有文件,使用以下代码即可:
1 function listdirfiles($dirpath){ 2 3 if($dir = opendir($dirpath)){ 4 5 while(($file = readdir($dir))!== false){ 6 7 if(!is_dir($dirpath.$file)) 8 9 { 10 11 echo filename: $file; 12 13 } 14 15 } 16 17 } 18 19 }20 21 //使用方法如下22 23 listdirfiles('home/some_folder/');
7、php获取当前页面url以下函数可以获取当前页面的url,不管是http还是https。
1 function curpageurl() { 2 3 $pageurl = 'http'; 4 5 if (!empty($_server['https'])) {$pageurl .= s;} 6 7 $pageurl .= ://; 8 9 if ($_server[server_port] != 80) { 10 11 $pageurl .= $_server[server_name].:.$_server[server_port].$_server12 [request_uri]; 13 14 } else { 15 16 $pageurl .= $_server[server_name].$_server[request_uri]; 17 18 } 19 20 return $pageurl; 21 22 }23 24 //使用方法如下25 26 echo curpageurl();
8、php强制下载文件有时我们不想让浏览器直接打开文件,如pdf文件,而是要直接下载文件,那么以下函数可以强制下载文件,函数中使用了application/octet-stream头类型。
1 function download($filename){ 2 3 if ((isset($filename))&&(file_exists($filename))){ 4 5 header(content-length: .filesize($filename)); 6 7 header('content-type: application/octet-stream'); 8 9 header('content-disposition: attachment; filename=' . $filename . ''); 10 11 readfile($filename); 12 13 } else { 14 15 echo looks like file does not exist!; 16 17 } 18 19 }20 21 //使用方法如下22 23 download('/down/test_45f73e852.zip');
9、php截取字符串长度我们经常会遇到需要截取字符串(含中文汉字)长度的情况,比如标题显示不能超过多少字符,超出的长度用…表示,以下函数可以满足你的需求。
1 /* 2 3 utf-8、gb2312都支持的汉字截取函数 4 5 cut_str(字符串, 截取长度, 开始长度, 编码); 6 7 编码默认为 utf-8 8 9 开始长度默认为 0 10 11 */ 12 13 function cutstr($string, $sublen, $start = 0, $code = 'utf-8'){ 14 15 if($code == 'utf-8'){ 16 17 $pa = /[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/; 18 19 preg_match_all($pa, $string, $t_string); 20 21 22 23 if(count($t_string[0]) - $start > $sublen) return join('', array_slice24 ($t_string[0], $start, $sublen))....; 25 26 return join('', array_slice($t_string[0], $start, $sublen)); 27 28 }else{ 29 30 $start = $start*2; 31 32 $sublen = $sublen*2; 33 34 $strlen = strlen($string); 35 36 $tmpstr = ''; 37 38 39 40 for($i=0; $i=$start && $i129){ 45 46 $tmpstr.= substr($string, $i, 2); 47 48 }else{ 49 50 $tmpstr.= substr($string, $i, 1); 51 52 } 53 54 } 55 56 if(ord(substr($string, $i, 1))>129) $i++; 57 58 } 59 60 if(strlen($tmpstr) 3600) { 4 5 $hours = intval($seconds / 3600); 6 7 $minutes = $seconds % 3600; 8 9 $time = $hours . : . gmstrftime('%m:%s', $minutes); 10 11 } else { 12 13 $time = gmstrftime('%h:%m:%s', $seconds); 14 15 } 16 17 return $time; 18 19 }20 21 //使用方法如下22 23 $seconds = 3712; 24 25 echo changetimetype($seconds);
http://www.bkjia.com/phpjc/923715.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/923715.htmltecharticle超实用php函数总结整理,实用php函数总结 1、php加密解密 php加密和解密函数可以用来加密一些有用的字符串存放在数据库里,并且通过可逆...
其它类似信息

推荐信息