PHP 12个常用功能的函数
程序员文章站
2022-06-22 16:38:21
...
demo1.php
<?php header('Content-Type: text/html; charset=utf-8'); //PHP加密解密 function encryptDecrypt($key, $string, $decrypt){ if($decrypt){ $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "12"); return $decrypted; }else{ $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key)))); return $encrypted; } } //加密: echo encryptDecrypt('demo', '网址:onestopweb.iteye.com',0),'<br>'; //解密: echo encryptDecrypt('demo', 'lONfy9/i2RmCh31P/cKFfM6Jd90ijfg0rhJr1GihF+o=',1); /* 输出效果: * lONfy9/i2RmCh31P/cKFfM6Jd90ijfg0rhJr1GihF+o= * 网址:onestopweb.iteye.com * */
demo2.php
<?php //PHP生成随机字符串 function generateRandomString($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, strlen($characters) - 1)]; } return $randomString; } echo generateRandomString(20); /* 输出效果: * 4e7ficXJk0jMvDQcCgZ8 * */
demo3.php
<?php //PHP获取文件扩展名(后缀) function getExtension($filename){ $myext = substr($filename, strrpos($filename, '.')); return str_replace('.','',$myext); } $filename = 'xxx.dd.gif'; echo getExtension($filename); /* 输出效果: * gif * */
demo4.php
<?php //PHP获取文件大小并格式化 function formatSize($size) { $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"); if ($size == 0) { return('n/a'); } else { return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); } } $thefile = filesize('demo.rar'); echo formatSize($thefile); /* 输出效果: * 114.14 KB * */
demo5.php
<?php header('Content-Type: text/html; charset=utf-8'); //PHP替换标签字符 function stringParser($string,$replacer){ $result = str_replace(array_keys($replacer), array_values($replacer),$string); return $result; } $string = '阅谁问君诵,水落清香浮。'; $replace_array = array('阅' => '睇','君' => '你','浮' => '沉'); echo stringParser($string,$replace_array); /* 输出效果: * 睇谁问你诵,水落清香沉。 * */
demo6.php
<?php //PHP列出目录下的文件名 function listDirFiles($DirPath){ if($dir = opendir($DirPath)){ while(($file = readdir($dir))!== false){ if(!is_dir($DirPath.$file)) { echo "filename: $file<br />"; } } } } listDirFiles('aaa'); /* 输出效果: * filename: bbb * filename: ccc * */
demo7.php
<?php //PHP获取当前页面URL //以下函数可以获取当前页面的URL,不管是http还是https。 function curPageURL() { $pageURL = 'http'; if (!isset($_SERVER['HTTPS'])) {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } echo curPageURL(); /* 输出效果: * http://192.168.0.180:84/demo7.php * */
demo8.php
<?php //PHP强制下载文件 //有时我们不想让浏览器直接打开文件,如PDF文件,而是要直接下载文件 //那么以下函数可以强制下载文件,函数中使用了application/octet-stream头类型。 function download($filename){ if ((isset($filename))&&(file_exists($filename))){ header("Content-length: ".filesize($filename)); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $filename . '"'); readfile("$filename"); } else { echo "Looks like file does not exist!"; } } download('demo.rar'); /* 输出效果: * 直接下载指定的文件 * */
demo9.php
<?php header('Content-Type: text/html; charset=utf-8'); //PHP截取字符串长度 //超过多少字符,超出的长度用…表示 function cutStr($string, $sublen, $start = 0, $code = 'UTF-8'){ if($code == 'UTF-8'){ $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]/"; preg_match_all($pa, $string, $t_string); if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."..."; return join('', array_slice($t_string[0], $start, $sublen)); }else{ $start = $start*2; $sublen = $sublen*2; $strlen = strlen($string); $tmpstr = ''; for($i=0; $i<$strlen; $i++){ if($i>=$start && $i<($start+$sublen)){ if(ord(substr($string, $i, 1))>129){ $tmpstr.= substr($string, $i, 2); }else{ $tmpstr.= substr($string, $i, 1); } } if(ord(substr($string, $i, 1))>129) $i++; } if(strlen($tmpstr)<$strlen ) $tmpstr.= "..."; return $tmpstr; } } $str = "阅谁问君诵,水落清香浮。"; echo cutStr($str,10); /* 输出效果: * 阅谁问君诵,水落清香... * */
demo10.php
<?php //PHP获取客户端真实IP function getIp() { if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) $ip = getenv("HTTP_CLIENT_IP"); else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) $ip = getenv("HTTP_X_FORWARDED_FOR"); else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) $ip = getenv("REMOTE_ADDR"); else if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) $ip = $_SERVER['REMOTE_ADDR']; else $ip = "unknown"; return ($ip); } echo getIp(); /* 输出效果: * 192.168.0.180 * */
demo11.php
<?php header('Content-Type: text/html; charset=utf-8'); //PHP页面提示与跳转 function message($msgTitle,$message,$jumpUrl){ $str = '<!DOCTYPE HTML>'; $str .= '<html>'; $str .= '<head>'; $str .= '<meta charset="utf-8">'; $str .= '<title>页面提示</title>'; $str .= '<style type="text/css">'; $str .= '*{margin:0; padding:0}a{color:#369; text-decoration:none;}a:hover{text-decoration:underline}body{height:100%; font:12px/18px Tahoma, Arial, sans-serif; color:#424242; background:#fff}.message{width:450px; height:120px; margin:16% auto; border:1px solid #99b1c4; background:#ecf7fb}.message h3{height:28px; line-height:28px; background:#2c91c6; text-align:center; color:#fff; font-size:14px}.msg_txt{padding:10px; margin-top:8px}.msg_txt h4{line-height:26px; font-size:14px}.msg_txt h4.red{color:#f30}.msg_txt p{line-height:22px}'; $str .= '</style>'; $str .= '</head>'; $str .= '<body>'; $str .= '<div>'; $str .= '<h3>'.$msgTitle.'</h3>'; $str .= '<div>'; $str .= '<h4>'.$message.'</h4>'; $str .= '<p>系统将在 <span style="color:blue;font-weight:bold">3</span> 秒后自动跳转,如果不想等待,直接点击 <a href="{$jumpUrl}">这里</a> 跳转</p>'; $str .= "<script>setTimeout('location.replace(\'".$jumpUrl."\')',2000)</script>"; $str .= '</div>'; $str .= '</div>'; $str .= '</body>'; $str .= '</html>'; echo $str; } message('操作提示','操作成功!','http://onestopweb.iteye.com/'); /* 输出效果: * 操作提示 * 操作成功! * 系统将在 3 秒后自动跳转,如果不想等待,直接点击 这里 跳转 * */
demo12.php
<?php //PHP计算时长 function changeTimeType($seconds) { if ($seconds > 3600) { $hours = intval($seconds / 3600); $minutes = $seconds % 3600; $time = $hours . ":" . gmstrftime('%M:%S', $minutes); } else { $time = gmstrftime('%H:%M:%S', $seconds); } return $time; } $seconds = 23; echo changeTimeType($seconds); /* 输出效果: * 00:00:23 * */
上一篇: vuepress锚点hash滚动问题
下一篇: 小白成长之路_LeetCode刷题记录