素材牛VIP会员
# PHP函数 #

收集整理一些常用的PHP自定义函数

项目中经常会需要一些让人头疼的函数,作为开发者应该整理一个自己的函数库,在需要之时复制过来即可。
收录资源 17 个 2022年11月14日 逸秋#AAS
资源列表(共17个) 新增资源

PHP列出指定目录下的全部文件名

2022年11月25日 综合评分:0 okpkda123

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

函数:

function listDirFiles($DirPath){
    if($dir = opendir($DirPath)){
         while(($file = readdir($dir))!== false){
                if(!is_dir($DirPath.$file))
                {
                    echo "filename: $file";
                }
         }
    }
}

示例:

listDirFiles('home/some_folder/');

PHP指定格式标签字符替换

2022年11月25日 综合评分:0 雪女@口月

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

函数:

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

PHP获取文件大小并格式化数值

2022年11月25日 综合评分:0 xiaoniuX

以下使用的函数可以获取文件的大小,并且转换成便于阅读的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);

PHP获取文件后缀扩展名

2022年11月25日 综合评分:0 长歌一曲sch

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

函数:

function getExtension($filename){
  $myext = substr($filename, strrpos($filename, '.'));
  return str_replace('.','',$myext);
}

示例:

$filename = '我的文档.doc';
echo getExtension($filename);

PHP字符串加密解密自定义函数

2022年11月14日 综合评分:0 风吹裤裆毛飞扬

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; 
    } 
}

使用方法:

//以下是将字符串“素材牛(www.sucainiu.com)”分别加密和解密
//加密:
echo encryptDecrypt('password', '素材牛(www.sucainiu.com)',0);
//解密:
echo encryptDecrypt('password', 'KMQY7BhdFF8oKh/yr3eekMB4tE8vbshodya9WnRpjVc=',1);

PHP图片压缩函数

2022年11月14日 综合评分:0 闪闪的红星

很多时候我们传图片由于图片过大需要压缩,但是常常压缩后图片会缺失不完整,这不是我们想要的效果,这个方法可以达到比较满意的效果,至少本人使用非常好,在减少图片大小的前提下能够保证图片的完整性。

函数:

//$imgsrc 图片地址
//$imgdst 压缩后地址
function compress_img($imgsrc, $imgdst) {
    list($width, $height, $type) = getimagesize($imgsrc);
    $new_width = $width;//压缩后的图片宽
    $new_height = $height;//压缩后的图片高
    if($width >= 600){
        $per = 600 / $width;//计算比例
        $new_width = $width * $per;
        $new_height = $height * $per;
    }
    switch ($type) {
        case 1:
            header('Content-Type:image/gif');
            $image_wp = imagecreatetruecolor($new_width, $new_height);
            $image = imagecreatefromgif($imgsrc);
            imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            //90代表的是质量、压缩图片容量大小
            imagejpeg($image_wp, $imgdst, 90);
            imagedestroy($image_wp);
            imagedestroy($image);
            break;
        case 2:
            header('Content-Type:image/jpeg');
            $image_wp = imagecreatetruecolor($new_width, $new_height);
            $image = imagecreatefromjpeg($imgsrc);
            imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            //90代表的是质量、压缩图片容量大小
            imagejpeg($image_wp, $imgdst, 90);
            imagedestroy($image_wp);
            imagedestroy($image);
            break;
        case 3:
            header('Content-Type:image/png');
            $image_wp = imagecreatetruecolor($new_width, $new_height);
            $image = imagecreatefrompng($imgsrc);
            imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            //90代表的是质量、压缩图片容量大小
            imagejpeg($image_wp, $imgdst, 90);
            imagedestroy($image_wp);
            imagedestroy($image);
            break;
    }
}

PHP找出一个字符串中的最长对称字符串

2022年11月14日 综合评分:0 linwenjiang

这个题我是第一次遇到,所以手写对我来说基本不可能,因为我遇到问题必须在机子上反复调试才可能得到最终答案,既然遇到了回来还是在电脑上试着写了一下,如有不足或者更简单的方法,欢迎指正。这个题的目的就是写一个函数找出字符串中对称的部分,然后获取符合条件中最长的那个字符串,比如字符串‘ssabcddcba’,那么符合条件的就是‘abcddcba’,下面直接上代码。

函数:

$str='aa6aaslolsbcdeggedcbaiokabccbanh';
function getMaxStr($str){
    $s_data=str_split($str);
    $row=[];
    foreach($s_data as $key=>$vol){
        foreach($s_data as $k=>$v){
            if($k<$key) continue;
            if($vol==$v && $key!=$k){
                if(($key+$k)==1){
                    $row[]=substr($str,$key,2);
                }else{
                    $px=$key==0?$k+1:$k-$key;
                    $px=($px%2)==0?$px:($k-$key+1);
                    $px=$px/2;
                    for($s=1;$s<$px;$s++){
                        if($s_data[$key+$s]!=$s_data[$k-$s]) continue 2;
                    }
                    $row[]=substr($str,$key,($k-$key+1));
                }
            }
        }
    }
    return $row;
}
$r=getMaxStr($str);
print_r($r);

运行结果:

Array
(
    [0] => aa
    [1] => aa6aa
    [2] => a6a
    [3] => aa
    [4] => slols
    [5] => lol
    [6] => bcdeggedcb
    [7] => cdeggedc
    [8] => degged
    [9] => egge
    [10] => gg
    [11] => abccba
    [12] => bccb
    [13] => cc
)
上一页12下一页