欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

非常实用的PHP常用函数汇总

程序员文章站 2023-03-26 13:19:43
本文实例总结了一些在php应用开发中常用到的函数,这些函数有字符操作,文件操作及其它的一些操作了,分享给大家供大家参考。具体如下: 1、php加密解密 php加密和解密...

本文实例总结了一些在php应用开发中常用到的函数,这些函数有字符操作,文件操作及其它的一些操作了,分享给大家供大家参考。具体如下:

1、php加密解密

php加密和解密函数可以用来加密一些有用的字符串存放在数据库里,并且通过可逆解密字符串,该函数使用了base64和md5加密和解密。

复制代码 代码如下:
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;
    }
}

使用方法如下:
复制代码 代码如下:
//以下是将字符串“helloweba欢迎您”分别加密和解密
//加密:
echo encryptdecrypt('password', 'helloweba欢迎您',0);
//解密:
echo encryptdecrypt('password', 'z0jax4qmwcf+db5tnbp/xwdum84snrsxvvpxuaca4bk=',1);

2、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);

3、php获取文件扩展名(后缀)

以下函数可以快速获取文件的扩展名即后缀。

复制代码 代码如下:
function getextension($filename){
  $myext = substr($filename, strrpos($filename, '.'));
  return str_replace('.','',$myext);
}

使用方法如下:
复制代码 代码如下:
$filename = '我的文档.doc';
echo getextension($filename);

4、php获取文件大小并格式化

以下使用的函数可以获取文件的大小,并且转换成便于阅读的kb,mb等格式。

复制代码 代码如下:
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('test_file.mp3');
echo formatsize($thefile);

5、php替换标签字符

有时我们需要将字符串、模板标签替换成指定的内容,可以用到下面的函数:

复制代码 代码如下:
function stringparser($string,$replacer){
    $result = str_replace(array_keys($replacer), array_values($replacer),$string);
    return $result;
}

使用方法如下:
复制代码 代码如下:
$string = 'the {b}anchor text{/b} is the {b}actual word{/b} or words used {br}to describe the link {br}itself';
$replace_array = array('{b}' => '<b>','{/b}' => '</b>','{br}' => '<br />');
 
echo stringparser($string,$replace_array);

6、php列出目录下的文件名

如果你想列出目录下的所有文件,使用以下代码即可:

复制代码 代码如下:
function listdirfiles($dirpath){
    if($dir = opendir($dirpath)){
         while(($file = readdir($dir))!== false){
                if(!is_dir($dirpath.$file))
                {
                    echo "filename: $file<br />";
                }
         }
    }
}

使用方法如下:
复制代码 代码如下:
listdirfiles('home/some_folder/');

7、php获取当前页面url

以下函数可以获取当前页面的url,不管是http还是https。

复制代码 代码如下:
function curpageurl() {
    $pageurl = 'http';
    if (!empty($_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();

8、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('/down/test_45f73e852.zip');

9、php截取字符串长度

我们经常会遇到需要截取字符串(含中文汉字)长度的情况,比如标题显示不能超过多少字符,超出的长度用...表示,以下函数可以满足你的需求。

复制代码 代码如下:
/*
 utf-8、gb2312都支持的汉字截取函数
 cut_str(字符串, 截取长度, 开始长度, 编码);
 编码默认为 utf-8
 开始长度默认为 0
*/
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 = "jquery插件实现的加载图片和页面效果";
echo cutstr($str,16);

10、php获取客户端真实ip

我们经常要用数据库记录用户的ip,以下代码可以获取客户端真实的ip:

复制代码 代码如下:
//获取用户真实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();

11、php防止sql注入

我们在查询数据库时,出于安全考虑,需要过滤一些非法字符防止sql恶意注入,请看一下函数:

复制代码 代码如下:
function injcheck($sql_str) { 
    $check = preg_match('/select|insert|update|delete|'|/*|*|../|./|union|into|load_file|outfile/', $sql_str);
    if ($check) {
        echo '非法字符!!';
        exit;
    } else {
        return $sql_str;
    }
}

使用方法如下:
复制代码 代码如下:
echo injcheck('1 or 1=1');

12、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 class="message">';
    $str .= '<h3>'.$msgtitle.'</h3>';
    $str .= '<div class="msg_txt">';
    $str .= '<h4 class="red">'.$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('操作提示','操作成功!','//www.jb51.net/');

13、php计算时长

我们在处理时间时,需要计算当前时间距离某个时间点的时长,如计算客户端运行时长,通常用hh:mm:ss表示。

复制代码 代码如下:
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 = 3712;
echo changetimetype($seconds);

希望本文所述对大家的php程序设计有所帮助。

下面特为大家加点料希望大家能喜欢:

本文汇总了常用的php函数,包括获取客户端ip,字符串截取,下载等,详情请查看如下代码:

<?php
/**
 * 获取客户端ip
 * @return [string] [description]
 */
function getclientip() {
 $ip = null;
 if (isset($_server['http_x_forwarded_for'])) {
  $arr = explode(',', $_server['http_x_forwarded_for']);
  $pos = array_search('unknown',$arr);
  if(false !== $pos) unset($arr[$pos]);
  $ip = trim($arr[0]);
 }elseif (isset($_server['http_client_ip'])) {
  $ip = $_server['http_client_ip'];
 }elseif (isset($_server['remote_addr'])) {
  $ip = $_server['remote_addr'];
 }
 // ip地址合法验证
 $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
 return $ip;
}

/**
 * 获取在线ip
 * @return string
 */
function getonlineip($format=0) {
 global $s_global;
 if(empty($s_global['onlineip'])) {
  if(getenv('http_client_ip') && strcasecmp(getenv('http_client_ip'), 'unknown')) {
   $onlineip = getenv('http_client_ip');
  } elseif(getenv('http_x_forwarded_for') && strcasecmp(getenv('http_x_forwarded_for'), 'unknown')) {
   $onlineip = getenv('http_x_forwarded_for');
  } elseif(getenv('remote_addr') && strcasecmp(getenv('remote_addr'), 'unknown')) {
   $onlineip = getenv('remote_addr');
  } elseif(isset($_server['remote_addr']) && $_server['remote_addr'] && strcasecmp($_server['remote_addr'], 'unknown')) {
   $onlineip = $_server['remote_addr'];
  }
  preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
  $s_global['onlineip'] = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
 }

 if($format) {
  $ips = explode('.', $s_global['onlineip']);
  for($i=0;$i<3;$i++) {
   $ips[$i] = intval($ips[$i]);
  }
  return sprintf('%03d%03d%03d', $ips[0], $ips[1], $ips[2]);
 } else {
  return $s_global['onlineip'];
 }
}



/**
 * 获取url
 * @return [type] [description]
 */
function geturl(){
 $pageurl = 'http';
 if (isset($_server["https"]) && $_server["https"] == "on") {
 $pageurl .= "s";
 }
 $pageurl .= "://";
 if ($_server["server_port"] != "80") {
 $pageurl .= $_server["http_host"] . ":" . $_server["server_port"] . $_server["request_uri"];
 } else {
 $pageurl .= $_server["http_host"] . $_server["request_uri"];
 }
 return $pageurl;
}

/**
 * 获取当前站点的访问路径根目录
 * @return [type] [description]
 */
function getsiteurl() {
 $uri = $_server['request_uri']?$_server['request_uri']:($_server['php_self']?$_server['php_self']:$_server['script_name']);
 return 'http://'.$_server['http_host'].substr($uri, 0, strrpos($uri, '/')+1);
}



/**
 * 字符串截取,支持中文和其他编码
 * @param [string] $str  [字符串]
 * @param integer $start [起始位置]
 * @param integer $length [截取长度]
 * @param string $charset [字符串编码]
 * @param boolean $suffix [是否有省略号]
 * @return [type]   [description]
 */
function msubstr($str, $start=0, $length=15, $charset="utf-8", $suffix=true) {
 if(function_exists("mb_substr")) {
  return mb_substr($str, $start, $length, $charset);
 } elseif(function_exists('iconv_substr')) {
  return iconv_substr($str,$start,$length,$charset);
 }
 $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
 $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
 $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
 $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
 preg_match_all($re[$charset], $str, $match);
 $slice = join("",array_slice($match[0], $start, $length));
 if($suffix) {
  return $slice."…";
 }
 return $slice;
}

/**
 * php 实现js escape 函数
 * @param [type] $string [description]
 * @param string $encoding [description]
 * @return [type]   [description]
 */
function escape($string, $encoding = 'utf-8'){
 $return = null;
 for ($x = 0; $x < mb_strlen($string, $encoding);$x ++)
 {
 $str = mb_substr($string, $x, 1, $encoding);
 if (strlen($str) > 1) { // 多字节字符
  $return .= "%u" . strtoupper(bin2hex(mb_convert_encoding($str, 'ucs-2', $encoding)));
 } else {
  $return .= "%" . strtoupper(bin2hex($str));
 }
 }
 return $return;
}
/**
 * php 实现 js unescape函数
 * @param [type] $str [description]
 * @return [type]  [description]
 */
function unescape($str) {
 $str = rawurldecode($str);
 preg_match_all("/(?:%u.{4})|.{4};|\d+;|.+/u",$str,$r);
 $ar = $r[0];
 foreach($ar as $k=>$v) {
  if(substr($v,0,2) == "%u"){
   $ar[$k] = iconv("ucs-2","utf-8//ignore",pack("h4",substr($v,-4)));
  } elseif(substr($v,0,3) == "") {
   $ar[$k] = iconv("ucs-2","utf-8",pack("h4",substr($v,3,-1)));
  } elseif(substr($v,0,2) == "") {
   echo substr($v,2,-1)."";
   $ar[$k] = iconv("ucs-2","utf-8",pack("n",substr($v,2,-1)));
  }
 }
 return join("",$ar);
}

/**
 * 数字转人名币
 * @param [type] $num [description]
 * @return [type]  [description]
 */
function num2rmb ($num) {
 $c1 = "零壹贰叁肆伍陆柒捌玖";
 $c2 = "分角元拾佰仟万拾佰仟亿";
 $num = round($num, 2);
 $num = $num * 100;
 if (strlen($num) > 10) {
  return "oh,sorry,the number is too long!";
 }
 $i = 0;
 $c = "";
 while (1) {
  if ($i == 0) {
   $n = substr($num, strlen($num)-1, 1);
  } else {
   $n = $num % 10;
  }
  $p1 = substr($c1, 3 * $n, 3);
  $p2 = substr($c2, 3 * $i, 3);
  if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
   $c = $p1 . $p2 . $c;
  } else {
   $c = $p1 . $c;
  }
  $i = $i + 1;
  $num = $num / 10;
  $num = (int)$num;
  if ($num == 0) {
   break;
  }
 }
 $j = 0;
 $slen = strlen($c);
 while ($j < $slen) {
  $m = substr($c, $j, 6);
  if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
   $left = substr($c, 0, $j);
   $right = substr($c, $j + 3);
   $c = $left . $right;
   $j = $j-3;
   $slen = $slen-3;
  }
  $j = $j + 3;
 }
 if (substr($c, strlen($c)-3, 3) == '零') {
  $c = substr($c, 0, strlen($c)-3);
 } // if there is a '0' on the end , chop it out
 return $c . "整";
}

/**
 * 特殊的字符
 * @param [type] $str [description]
 * @return [type]  [description]
 */
function makesemiangle($str) {
 $arr = array(
  '0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
  '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
  'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 'e' => 'e',
  'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i', 'j' => 'j',
  'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n', 'o' => 'o',
  'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', 't' => 't',
  'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x', 'y' => 'y',
  'z' => 'z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
  'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
  'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
  'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
  't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
  'y' => 'y', 'z' => 'z',
  '(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
  '】' => ']', '〖' => '[', '〗' => ']', '{' => '{', '}' => '}', '《' => '<',
  '》' => '>',
  '%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',
  ':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',
  ';' => ';', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',
  '”' => '"', '“' => '"', ''' => '`', '‘' => '`', '|' => '|', '〃' => '"',
  ' ' => ' ','.' => '.');
 return strtr($str, $arr);
}

/**
 * 下载
 * @param [type] $filename [description]
 * @param string $dir  [description]
 * @return [type]   [description]
 */
function downloads($filename,$dir='./'){
 $filepath = $dir.$filename;
 if (!file_exists($filepath)){
  header("content-type: text/html; charset=utf-8");
  echo "file not found!";
  exit;
 } else {
  $file = fopen($filepath,"r");
  header("content-type: application/octet-stream");
  header("accept-ranges: bytes");
  header("accept-length: ".filesize($filepath));
  header("content-disposition: attachment; filename=".$filename);
  echo fread($file, filesize($filepath));
  fclose($file);
 }
}

/**
 * 创建一个目录树
 * @param [type] $dir [description]
 * @param integer $mode [description]
 * @return [type]  [description]
 */
function mkdirs($dir, $mode = 0777) {
 if (!is_dir($dir)) {
  mkdirs(dirname($dir), $mode);
  return mkdir($dir, $mode);
 }
 return true;
}