Fun.php
1.1 KB
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
53
54
55
56
57
58
59
<?php
namespace Lib\Imap;
/**
 * @author:dc
 * @time 2024/10/21 9:47
 * Class Fun
 * @package Lib\Imap
 */
class Fun {
    /**
     * 编码
     * @param $str
     * @param $to
     * @param null $from
     * @return string
     * @author:dc
     * @time 2024/10/21 9:43
     */
    public static function mb_convert_encoding($str, $to, $from = null){
        $from = self::getEncodingAliases($from);
        try {
            return mb_convert_encoding($str,$to,$from);
        }catch (\Throwable $e){}
        return mb_convert_encoding($str,$to);
    }
    /**
     * 只有发现一个处理一个
     * 获取编码
     * @author:dc
     * @time 2024/10/25 17:24
     */
    public static function getEncodingAliases($coding){
        $alias = [
            "iso-8859-8-i"             => "ISO-8859-8",
            "iso-8859-8-e"             => "ISO-8859-8",
            "iso-8859-6-i"             => "ISO-8859-6",
            "iso-8859-6-e"             => "ISO-8859-6",
        ];
        $coding = strtolower($coding);
        if(isset($alias[$coding])){
            return $alias[$coding];
        }
        return $coding;
    }
}