Rfc822AddressesParser
RFC 822 Email address list validation Utility
What is it?
This class will take an address string, and parse it into it's constituent parts, be that either addresses, groups, or combinations. Nested groups are not supported. The structure it returns is pretty straight forward, and is similar to that provided by the imap_rfc822_parse_adrlist(). Use print_r() to view the structure.
How do I use it?
$address_string = 'My Group: "Richard" <richard@localhost> (A comment), ted@example.com (Ted Bloggs), Barney;';
$structure = Mail_RFC822::parseAddressList($address_string, 'example.com', TRUE)
print_r($structure);
Tags
Table of Contents
Methods
- __construct() : mixed
- Sets up the object.
- parseAddressList() : array<string|int, mixed>
- Starts the whole process. The address must either be set here or when creating the object. One or the other.
- _hasUnclosedBrackets() : bool
- Checks if a string has an unclosed brackets or not. IMPORTANT: This function handles both angle brackets and square brackets;
- _hasUnclosedBracketsSub() : int
- Sub function that is used only by hasUnclosedBrackets().
- _hasUnclosedQuotes() : bool
- Checks if a string has unclosed quotes or not.
- _isGroup() : bool
- Checks for a group at the start of the string.
- _splitAddresses() : string|false
- Splits an address into separate addresses.
- _splitCheck() : mixed
- A common function that will check an exploded string.
- _validateAddress() : mixed
- Function to begin checking the address.
- _validateAddrSpec() : mixed
- Function to validate an addr-spec.
- _validateAtom() : bool
- Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs>
- _validateDliteral() : bool
- Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]"
- _validateDomain() : mixed
- Function to validate a domain, though this is not quite what you expect of a strict internet domain.
- _validateLocalPart() : mixed
- Function to validate the local part of an address: local-part = word *("." word)
- _validatePhrase() : bool
- Function to validate a phrase.
- _validateQuotedString() : bool
- Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <">
- _validateRoute() : string|bool
- Function to validate a route, which is: route = 1#("@" domain) ":"
- _validateRouteAddr() : mixed
- This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">"
- _validateSubdomain() : bool
- Function to validate a subdomain: subdomain = domain-ref / domain-literal
- validateMailbox() : bool
- Function to validate a mailbox, which is: mailbox = addr-spec ; simple address phrase route-addr ; name and route-addr
Methods
__construct()
Sets up the object.
public
__construct([string $address = null ][, string $default_domain = null ][, bool $validate = null ][, int $limit = null ]) : mixed
Parameters
- $address : string = null
-
The address(es) to validate.
- $default_domain : string = null
-
Default domain/host etc. If not supplied, will be set to localhost.
- $validate : bool = null
-
Whether to validate atoms. Turn this off if you need to run addresses through before encoding the personal names, for instance.
- $limit : int = null
parseAddressList()
Starts the whole process. The address must either be set here or when creating the object. One or the other.
public
parseAddressList([string $address = null ][, string $default_domain = null ][, bool $validate = null ][, int $limit = null ]) : array<string|int, mixed>
Parameters
- $address : string = null
-
The address(es) to validate.
- $default_domain : string = null
-
Default domain/host etc.
- $validate : bool = null
-
Whether to validate atoms. Turn this off if you need to run addresses through before encoding the personal names, for instance.
- $limit : int = null
Return values
array<string|int, mixed> —A structured array of addresses.
_hasUnclosedBrackets()
Checks if a string has an unclosed brackets or not. IMPORTANT: This function handles both angle brackets and square brackets;
protected
_hasUnclosedBrackets(string $string, string $chars) : bool
Parameters
- $string : string
-
The string to check.
- $chars : string
-
The characters to check for.
Return values
bool —TRUE if there are unclosed brackets inside the string, FALSE otherwise.
_hasUnclosedBracketsSub()
Sub function that is used only by hasUnclosedBrackets().
protected
_hasUnclosedBracketsSub(string $string, int &$num, string $char) : int
Parameters
- $string : string
-
The string to check.
- $num : int
-
The number of occurrences.
- $char : string
-
The character to count.
Return values
int —The number of occurrences of $char in $string, adjusted for backslashes.
_hasUnclosedQuotes()
Checks if a string has unclosed quotes or not.
protected
_hasUnclosedQuotes(string $string) : bool
Parameters
- $string : string
-
The string to check.
Return values
bool —TRUE if there are unclosed quotes inside the string,
_isGroup()
Checks for a group at the start of the string.
protected
_isGroup(string|false $address) : bool
Parameters
- $address : string|false
-
The address to check.
Return values
bool —Whether or not there is a group at the start of the string.
_splitAddresses()
Splits an address into separate addresses.
protected
_splitAddresses(string|false $address) : string|false
Parameters
- $address : string|false
-
The addresses to split.
Return values
string|false —False on failure.
_splitCheck()
A common function that will check an exploded string.
protected
_splitCheck(array<string|int, mixed> $parts, string $char) : mixed
Parameters
- $parts : array<string|int, mixed>
-
The exploded string.
- $char : string
-
The char that was exploded on.
Return values
mixed —False if the string contains unclosed quotes/brackets, or the string on success.
_validateAddress()
Function to begin checking the address.
protected
_validateAddress(string $address) : mixed
Parameters
- $address : string
-
The address to validate.
Return values
mixed —False on failure, or a structured array of address information on success.
_validateAddrSpec()
Function to validate an addr-spec.
protected
_validateAddrSpec(string $addr_spec) : mixed
addr-spec = local-part "@" domain
Parameters
- $addr_spec : string
-
The string to check.
Return values
mixed —False on failure, or the validated addr-spec on success.
_validateAtom()
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs>
protected
_validateAtom(string $atom) : bool
If validation ($this->validate) has been turned off, then validateAtom() doesn't actually check anything. This is so that you can split a list of addresses up before encoding personal names (umlauts, etc.), for example.
Parameters
- $atom : string
-
The string to check.
Return values
bool —Success or failure.
_validateDliteral()
Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]"
protected
_validateDliteral(string $dliteral) : bool
Parameters
- $dliteral : string
-
The string to check.
Return values
bool —Success or failure.
_validateDomain()
Function to validate a domain, though this is not quite what you expect of a strict internet domain.
protected
_validateDomain(string $domain) : mixed
domain = sub-domain *("." sub-domain)
Parameters
- $domain : string
-
The string to check.
Return values
mixed —False on failure, or the validated domain on success.
_validateLocalPart()
Function to validate the local part of an address: local-part = word *("." word)
protected
_validateLocalPart(string $local_part) : mixed
Parameters
- $local_part : string
Return values
mixed —False on failure, or the validated local part on success.
_validatePhrase()
Function to validate a phrase.
protected
_validatePhrase(string $phrase) : bool
Parameters
- $phrase : string
-
The phrase to check.
Return values
bool —Success or failure.
_validateQuotedString()
Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <">
protected
_validateQuotedString(string $qstring) : bool
Parameters
- $qstring : string
-
The string to check
Return values
bool —Success or failure.
_validateRoute()
Function to validate a route, which is: route = 1#("@" domain) ":"
protected
_validateRoute(string $route) : string|bool
Parameters
- $route : string
-
The string to check.
Return values
string|bool —False on failure, or the validated $route on success.
_validateRouteAddr()
This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">"
protected
_validateRouteAddr(string $route_addr) : mixed
Angle brackets have already been removed at the point of getting to this function.
Parameters
- $route_addr : string
-
The string to check.
Return values
mixed —False on failure, or an array containing validated address/route information on success.
_validateSubdomain()
Function to validate a subdomain: subdomain = domain-ref / domain-literal
protected
_validateSubdomain(string $subdomain) : bool
Parameters
- $subdomain : string
-
The string to check.
Return values
bool —Success or failure.
validateMailbox()
Function to validate a mailbox, which is: mailbox = addr-spec ; simple address phrase route-addr ; name and route-addr
protected
validateMailbox(string &$mailbox) : bool
Parameters
- $mailbox : string
-
The string to check.
Return values
bool —Success or failure.