Oihana PHP Commands

assertDomain.php

Table of Contents

Functions

assertDomain()  : bool
Validates a domain name according to RFC rules and common DNS restrictions.

Functions

assertDomain()

Validates a domain name according to RFC rules and common DNS restrictions.

assertDomain(string $domain[, bool $throw = true ][, bool $requireTld = true ]) : bool
Parameters
$domain : string

The domain name to validate (e.g. "example.com").

$throw : bool = true

Whether to throw an exception on invalid domain.

$requireTld : bool = true

Whether to require a TLD (default true).

Tags
throws
InvalidArgumentException

If the domain is invalid and $throw is true.

author

Marc Alcaraz (ekameleon)

since
1.0.0
example
use function oihana\commands\helpers\assertDomain;

try
{
   assertDomain("example.com");         // returns true
   assertDomain("sub.domain.org");      // returns true
   assertDomain("invalid_domain");      // throws InvalidArgumentException
}
catch (InvalidArgumentException $e)
{
   echo "Invalid domain: " . $e->getMessage();
}

// Without exception throwing
$isValid = assertDomain("invalid_domain", false);  // returns false
Return values
bool

True if valid (or silent if $throw = true and valid).


        
On this page

Search results