cos1.3 - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of cos1.3.php

  1. <?php
  2. class emailValidator {
  3.     /**
  4.      * @var string the regular expression used to validate the attribute value.
  5.      * @see http://www.regular-expressions.info/email.html
  6.      */
  7.     public $pattern = '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/';
  8.     /**
  9.      * @var bool whether to check whether the email's domain exists and has either an A or MX record.
  10.      * Be aware that this check can fail due to temporary DNS problems even if the email address is
  11.      * valid and an email would be deliverable. Defaults to false.
  12.      */
  13.     public $checkDNS = false;
  14.     /**
  15.      * @var bool whether validation process should take into account IDN (internationalized domain
  16.      * names). Defaults to false meaning that validation of emails containing IDN will always fail.
  17.      * Note that in order to use IDN validation you have to install and enable `intl` PHP extension,
  18.      * otherwise an exception would be thrown.
  19.      */
  20.     public $enableIDN = false;
  21.     public function __construct($checkDNS = false, $enableIDN = false) {
  22.         $this->checkDNS = $checkDNS;
  23.         $this->enableIDN = $enableIDN;
  24.         if ($enableIDN && !function_exists('idn_to_ascii')) {
  25.             throw new \Exception('In order to use IDN validation intl extension must be installed and enabled.');
  26.         }
  27.     }
  28.     public function validate($value) {
  29.         if (!is_string($value)) {
  30.             $valid = false;
  31.         } elseif (!preg_match('/^(?P<name>(?:"?([^"]*)"?\s)?)(?:\s+)?(?:(?P<open><?)((?P<local>.+)@(?P<domain>[^>]+))(?P<close>>?))$/i', $value, $matches)) {
  32.             $valid = false;
  33.         } else {
  34.             if ($this->enableIDN) {
  35.                 $matches['local'] = $this->idnToAscii($matches['local']);
  36.                 $matches['domain'] = $this->idnToAscii($matches['domain']);
  37.                 $value = $matches['name'] . $matches['open'] . $matches['local'] . '@' . $matches['domain'] . $matches['close'];
  38.             }
  39.             if (strlen($matches['local']) > 64) {
  40.                 // The maximum total length of a user name or other local-part is 64 octets. RFC 5322 section 4.5.3.1.1
  41.                 // http://tools.ietf.org/html/rfc5321#section-4.5.3.1.1
  42.                 $valid = false;
  43.             } elseif (strlen($matches['local'] . '@' . $matches['domain']) > 254) {
  44.                 // There is a restriction in RFC 2821 on the length of an address in MAIL and RCPT commands
  45.                 // of 254 characters. Since addresses that do not fit in those fields are not normally useful, the
  46.                 // upper limit on address lengths should normally be considered to be 254.
  47.                 //
  48.                 // Dominic Sayers, RFC 3696 erratum 1690
  49.                 // http://www.rfc-editor.org/errata_search.php?eid=1690
  50.                 $valid = false;
  51.             } else {
  52.                 $valid = preg_match($this->pattern, $value);
  53.                 if ($valid && $this->checkDNS) {
  54.                     $valid = $this->isDNSValid($matches['domain']);
  55.                 }
  56.             }
  57.         }
  58.         return ($valid===false)false : true;
  59.     }
  60.     protected function isDNSValid($domain) {
  61.         return $this->hasDNSRecord($domain, true) || $this->hasDNSRecord($domain, false);
  62.     }
  63.     private function hasDNSRecord($domain, $isMX) {
  64.         $normalizedDomain = $domain . '.';
  65.         if (!checkdnsrr($normalizedDomain, ($isMX 'MX' : 'A'))) {
  66.             return false;
  67.         }
  68.         try {
  69.             // dns_get_record can return false and emit Warning that may or may not be converted to ErrorException
  70.             $records = dns_get_record($normalizedDomain, ($isMX ? DNS_MX : DNS_A));
  71.         } catch (ErrorException $exception) {
  72.             return false;
  73.         }
  74.         return !empty($records);
  75.     }
  76.     private function idnToAscii($idn) {
  77.         if (PHP_VERSION_ID < 50600) {
  78.             return idn_to_ascii($idn);
  79.         }
  80.         return idn_to_ascii($idn, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
  81.     }
  82. }
  83. /*
  84.  * USING
  85.  */
  86. // validate just by regex
  87. $validator = new emailValidator();
  88. $check = $validator->validate('[email protected]');
  89. var_dump($check); // return true
  90. $check = $validator->validate('test.com');
  91. var_dump($check); // return false
  92. // validate by regex and DNS records
  93. $validator = new emailValidator(true);
  94. $check = $validator->validate('[email protected]');
  95. var_dump($check); // return true or false
  96. // validate by regex, DNS records and IDN
  97. $validator = new emailValidator(true, true);
  98. $check = $validator->validate('[email protected]');
  99. var_dump($check); // return true or false
  100. cos 6.3 
  101.  
  102. dau zin 
  103.  
File Description
  • cos1.3
  • PHP Code
  • 07 Feb-2024
  • 4.59 Kb
You can Share it: