【PHP】个人常用的封装函数和方法
Prism's Blog Lv2

一些比较常用的 函数封装 :c

PDO数据库链接方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function SQL($event){
// 链接数据库 使用PDO的方法
$dsn = 'mysql:dbname=[表名];host=[IP]';
$pdo = new PDO($dsn,'[账户]','[密码]');
// 传过来的语句
$sth = $pdo->prepare($event);
// 执行语句
$sth->execute();
// 返回数据
echo $sth->fetchAll();
// 关闭链接
$sth = null;
$pdo = null;
}

生成乱序卡密

需要传一个长度数值

1
2
3
4
5
6
7
8
function randKey($len) {
$chars='123456789';
mt_srand((double)microtime()*1000000*getmypid());
$password='';
while(strlen($password)<$len)
$password.=substr($chars,(mt_rand()%strlen($chars)),1);
return $password;
}

Curl 请求方法封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function http_query($url, $get = null, $post = null)
{
if (isset($get)) {
if (substr_count($url, '?') > 0) {
$url .= "&" . http_build_query($get);
} else {
$url .= "?" . http_build_query($get);
}
}
// 初始化一个cURL会话
$ch = curl_init($url);
if (isset($post)) {
curl_setopt($ch, CURLOPT_POST, TRUE); #开启post
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); #post数据
}
curl_setopt($ch, CURLOPT_HEADER, 0); #是否需要头部信息(否)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); #如果成功只将结果返回,不自动输出任何内容。
curl_setopt($ch, CURLOPT_TIMEOUT, 5); #设置允许执行的最长秒数。
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); #在发起连接前等待的时间,如果设置为0,则无限等待。
//忽略证书
if (substr($url, 0, 5) == 'https') {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
}
$curl_result = curl_exec($ch);
if ($curl_result) {
$data = $curl_result;
} else {
$data = curl_error($ch);
}
curl_close($ch); #关闭cURL会话
return $data;
}

硬核过滤字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function strFilter($str){
$str = str_replace('`', '', $str);
$str = str_replace('·', '', $str);
$str = str_replace('~', '', $str);
$str = str_replace('!', '', $str);
$str = str_replace('!', '', $str);
$str = str_replace('@', '', $str);
$str = str_replace('#', '', $str);
$str = str_replace('$', '', $str);
$str = str_replace('¥', '', $str);
$str = str_replace('%', '', $str);
$str = str_replace('^', '', $str);
$str = str_replace('……', '', $str);
$str = str_replace('&', '', $str);
$str = str_replace('*', '', $str);
$str = str_replace('(', '', $str);
$str = str_replace(')', '', $str);
$str = str_replace('(', '', $str);
$str = str_replace(')', '', $str);
$str = str_replace('-', '', $str);
$str = str_replace('+', '', $str);
$str = str_replace('=', '', $str);
$str = str_replace('|', '', $str);
$str = str_replace('\\', '', $str);
$str = str_replace('[', '', $str);
$str = str_replace(']', '', $str);
$str = str_replace('【', '', $str);
$str = str_replace('】', '', $str);
$str = str_replace('{', '', $str);
$str = str_replace('}', '', $str);
$str = str_replace(';', '', $str);
$str = str_replace(';', '', $str);
$str = str_replace(':', '', $str);
$str = str_replace(':', '', $str);
$str = str_replace('\'', '', $str);
$str = str_replace('"', '', $str);
$str = str_replace('“', '', $str);
$str = str_replace('”', '', $str);
$str = str_replace(',', '', $str);
$str = str_replace(',', '', $str);
$str = str_replace('<', '', $str);
$str = str_replace('>', '', $str);
$str = str_replace('《', '', $str);
$str = str_replace('》', '', $str);
$str = str_replace('.', '', $str);
$str = str_replace('。', '', $str);
$str = str_replace('/', '', $str);
$str = str_replace('、', '', $str);
$str = str_replace('?', '', $str);
$str = str_replace('?', '', $str);
return trim($str);
}

简单易懂的图片封装

应该算封装 LMAO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 可以传入一个名字为图片保存的名字
function UploadImg($fill_name){
// 允许上传的图片后缀
$allowedExts = array("webp", "jpg", "png");
// 分割字符串为数组 xxx.png 变成了 ['xxx'] ['png']
$temp = explode(".", $_FILES["file"]["name"]);
// end取数组最后一个值
$extension = end($temp); // 获取文件后缀名
// 判断文件类型
if ((($_FILES["file"]["type"] == "image/webp")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 1024000) // 小于 200 kb
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
return "错误:" . $_FILES["file"]["error"];
}
else
{
// 判断是否有同名文件
if (file_exists("./$fill_name.$extension"))
{
return "文件/ID已存在,请勿重复上传";
}
else
{
// 路径 将缓存的文件复制到某个路径里
move_uploaded_file($_FILES["file"]["tmp_name"], "./$fill_name.$extension");
return "成功上传文件";
}
}
}
else
{
return "文件格式只允许 jpg png webp 或 文件大小超过1M";
}
}

常用的方法

JSON 处理

1
2
3
4
5
6
7
8
9
// 将数组转为json
json_encode( 数组, 常量 | 常量 );
// 常用的常量
// JSON_UNESCAPED_UNICODE 不会把中文转为unicode
// JSON_UNESCAPED_SLASHES 不转义反斜杠
// JSON_PRETTY_PRINT 格式化输出的json

// 解析json为数组
json_decode(JSON,true);
  • 本文标题:【PHP】个人常用的封装函数和方法
  • 本文作者:Prism's Blog
  • 创建时间:2022-12-13 13:55:24
  • 本文链接:https://blog.msirp.cn/2022/12/13/【PHP】个人常用的封装函数/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!