valid_email($email) === false) { return false; } } return true; } //-------------------------------------------------------------------- /** * Validate an IP address * * @param string $ip IP Address * @param string $which IP protocol: 'ipv4' or 'ipv6' * @param array $data * * @return boolean */ public function valid_ip(string $ip = null, string $which = null, array $data): bool { switch (strtolower($which)) { case 'ipv4': $which = FILTER_FLAG_IPV4; break; case 'ipv6': $which = FILTER_FLAG_IPV6; break; default: $which = null; break; } return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which); } //-------------------------------------------------------------------- /** * Checks a URL to ensure it's formed correctly. * * @param string $str * * @return boolean */ public function valid_url(string $str = null): bool { if (empty($str)) { return false; } elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches)) { if (! in_array($matches[1], ['http', 'https'], true)) { return false; } $str = $matches[2]; } $str = 'http://' . $str; return (filter_var($str, FILTER_VALIDATE_URL) !== false); } //-------------------------------------------------------------------- /** * Checks for a valid date and matches a given date format * * @param string $str * @param string $format * * @return boolean */ public function valid_date(string $str = null, string $format = null): bool { if (empty($format)) { return (bool) strtotime($str); } $date = \DateTime::createFromFormat($format, $str); return (bool) $date && \DateTime::getLastErrors()['warning_count'] === 0 && \DateTime::getLastErrors()['error_count'] === 0; } //-------------------------------------------------------------------- }