Skip to content

Instantly share code, notes, and snippets.

@bramstroker
Created October 1, 2014 14:47
Show Gist options
  • Save bramstroker/ed6a45add5ee3aad4ad0 to your computer and use it in GitHub Desktop.
Save bramstroker/ed6a45add5ee3aad4ad0 to your computer and use it in GitHub Desktop.
protected function validateLocalPart()
{
// First try to match the local part on the common dot-atom format
$result = false;
// Dot-atom characters are: 1*atext *("." 1*atext)
// atext: ALPHA / DIGIT / and "!", "#", "$", "%", "&", "'", "*",
// "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~"
$atext = 'a-zA-Z0-9\x21\x23\x24\x25\x26\x27\x2a\x2b\x2d\x2f\x3d\x3f\x5e\x5f\x60\x7b\x7c\x7d\x7e';
if (preg_match('/^[' . $atext . ']+(\x2e+[' . $atext . ']+)*$/', $this->localPart)) {
$result = true;
} else {
// Try quoted string format (RFC 5321 Chapter 4.1.2)
// Quoted-string characters are: DQUOTE *(qtext/quoted-pair) DQUOTE
$qtext = '\x20-\x21\x23-\x5b\x5d-\x7e'; // %d32-33 / %d35-91 / %d93-126
$quotedPair = '\x20-\x7e'; // %d92 %d32-126
if (preg_match('/^"(['. $qtext .']|\x5c[' . $quotedPair . '])*"$/', $this->localPart)) {
$result = true;
} else {
$this->error(self::DOT_ATOM);
$this->error(self::QUOTED_STRING);
$this->error(self::INVALID_LOCAL_PART);
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment