Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F97590312
PhutilEmailAddress.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Jan 5, 12:58
Size
2 KB
Mime Type
text/x-php
Expires
Tue, Jan 7, 12:58 (2 d)
Engine
blob
Format
Raw Data
Handle
23432067
Attached To
rPHU libphutil
PhutilEmailAddress.php
View Options
<?php
/**
* Basic email address parser. This parser is very liberal and does not attempt
* to be fully RFC-compliant, because trying to do so is a crazy mess. However,
* it should parse all reasonable addresses which are actually in use on the
* internet today.
*
* @group util
*/
final
class
PhutilEmailAddress
{
private
$displayName
;
private
$localPart
;
private
$domainName
;
public
function
__construct
(
$email_address
)
{
$email_address
=
trim
(
$email_address
);
$matches
=
null
;
if
(
preg_match
(
'/^(.*)<(.*?)>$/'
,
$email_address
,
$matches
))
{
$display_name
=
trim
(
$matches
[
1
],
'
\'
" '
);
if
(
strpos
(
$matches
[
2
],
'@'
)
!==
false
)
{
list
(
$local_part
,
$domain_name
)
=
explode
(
'@'
,
$matches
[
2
],
2
);
}
else
{
$local_part
=
$matches
[
2
];
$domain_name
=
null
;
}
}
else
if
(
preg_match
(
'/^(.*)@(.*)$/'
,
$email_address
,
$matches
))
{
$display_name
=
null
;
$local_part
=
$matches
[
1
];
$domain_name
=
$matches
[
2
];
}
else
{
$display_name
=
null
;
$local_part
=
$email_address
;
$domain_name
=
null
;
}
$this
->
displayName
=
$display_name
;
$this
->
localPart
=
$local_part
;
$this
->
domainName
=
$domain_name
;
}
public
function
__toString
()
{
$address
=
$this
->
getAddress
();
if
(
$this
->
displayName
)
{
return
$this
->
displayName
.
' <'
.
$address
.
'>'
;
}
else
{
return
$address
;
}
}
public
function
setDisplayName
(
$display_name
)
{
$this
->
displayName
=
$display_name
;
return
$this
;
}
public
function
getDisplayName
()
{
return
$this
->
displayName
;
}
public
function
setLocalPart
(
$local_part
)
{
$this
->
localPart
=
$local_part
;
return
$this
;
}
public
function
getLocalPart
()
{
return
$this
->
localPart
;
}
public
function
setDomainName
(
$domain_name
)
{
$this
->
domainName
=
$domain_name
;
return
$this
;
}
public
function
getDomainName
()
{
return
$this
->
domainName
;
}
public
function
getAddress
()
{
$address
=
$this
->
localPart
;
if
(
$this
->
domainName
)
{
$address
.=
'@'
.
$this
->
domainName
;
}
return
$address
;
}
}
Event Timeline
Log In to Comment