Address.php
1.7 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace Lib\Imap\Parse;
/**
* @author:dc
* @time 2024/9/11 11:17
* Class Address
* @package Imap\Parse
*/
class Address {
public string $name = '';
public string $email = '';
private string $raw;
private function __construct(string $address){
$this->raw = $address;
if($this->raw){
$this->parse();
}
}
/**
* 创建一个地址类
* @param string $address
* @return static
* @author:dc
* @time 2024/9/11 11:37
*/
public static function make(string $address):self {
return new self($address);
}
/**
* 解析地址
* @author:dc
* @time 2024/9/11 11:39
*/
private function parse(){
$email = self::pregEmail($this->raw);
if(!empty($email)){
$this->email = $email;
$this->name = trim(str_replace([$email,'"','<','>','>','<'],'',$this->raw));
}
if($this->name){
$this->name = DeCode::decode($this->name);
}else{
$this->name = explode('@',$this->email)[0]??'';
}
}
/**
* 匹配邮箱
* @param $str
* @return string
* @author:dc
* @time 2024/9/11 11:43
*/
private function pregEmail(string $str):string {
preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/',$str,$email);
if(empty($email[0])){
// 邮箱2
preg_match('/[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/',$str,$email);
}
return str_replace(['<','>'],'',$email[0]??'');
}
/**
* @return string
*/
public function getRaw(): string
{
return $this->raw;
}
}