Introducing SilvarCode Domain Parser: A Simple and Accurate PHP Tool for Domain Validation
In the realm of web development, accurately parsing and validating domain names is crucial, especially when dealing with complex TLDs and subdomains. The SilvarCode Domain Parser offers a lightweight PHP solution that leverages the official Public Suffix List to ensure precise domain validation, making it an indispensable tool for modern web applications.

Jun 07, 2025
4 minutes

What Is SilvarCode Domain Parser?
SilvarCode Domain Parser is an open-source PHP package engineered to deliver precise and developer-friendly domain name parsing and validation. Leveraging the authoritative Public Suffix List, it accurately differentiates between public suffixes, registrable domains, and subdomains.
In web development, domain validation is often underestimated. Relying on simplistic regular expressions or hard-coded logic can lead to erroneous parsing, creating security risks or functional bugs—especially with internationalized domains or complex TLDs such as .co.uk.
SilvarCode Domain Parser addresses these challenges with a clean, testable, and PSR-compliant architecture. It is lightweight, dependency-free, and suitable for both standalone scripts and full-stack applications.
What Does It Do?
The parser provides several essential capabilities:
- Validates domain names using syntactic rules combined with the public suffix list.
- Detects whether a domain is a subdomain.
- Extracts the subdomain portion when present.
- Identifies the registrable domain (e.g., example.co.uk from blog.example.co.uk).
- Parses domain components following established standards for accuracy.
Key Methods
The core API includes:
tld(string $host): ?string
Returns the top-level domain (public suffix), for example co.uk from blog.example.co.uk.
getRegistrableDomain(string $host): ?string
Returns the registrable domain such as example.com or example.co.uk. Returns null if the domain is invalid or the TLD is unrecognized.
getSubdomain(string $host): ?string
Returns the leftmost subdomain (e.g., blog in blog.example.com), or null if none exists.
getSubdomains(string $host): array
Returns all subdomains as an array, for example ['blog', 'dev'] from dev.blog.example.com. Returns an empty array if no subdomains are present.
getProcessedSuffixFilePath(): string
Returns the full path to the locally cached suffix list file used for lookups.
showResult(array $info): void
Outputs structured domain data for CLI testing and debugging.
All methods are deterministic, side-effect free, and designed for consistent output in PHP 8.2+ environments.
Why Use It?
Domain parsing is vital in numerous PHP applications, including login restrictions, tenant identification, email validation, and domain-specific access control.
SilvarCode Domain Parser offers several key advantages:
- Standards-compliant: Uses the official Public Suffix List to ensure precise, current domain parsing.
- Framework-agnostic: Fully PSR-4 compliant, enabling seamless integration into any PHP project.
- Lightweight: Minimal footprint with zero external dependencies, totaling under 200 KB of source code.
- Efficient: Optional in-memory caching enhances performance by reducing redundant parsing operations.
- Transparent: Open-source with thorough documentation for ease of use and customization.
These qualities make SilvarCode Domain Parser an excellent choice for libraries, plugins, and frameworks requiring robust domain handling.
How to Install
Install the package via Composer:
composer require silvarcode/domain-parser
This package requires PHP 8.2 or newer and adheres to PSR-4 autoloading standards. No additional configuration is necessary. After installation, the parser is immediately ready for use:
use SilvarCode\DomainParser\DomainParser;
$parser = new DomainParser();
Basic Usage Example
Example of instantiating and using SilvarCode Domain Parser:
use SilvarCode\DomainParser\Parser\DomainParser; // Instantiate parser with memory caching enabled $parser = new DomainParser(true); // Instantiate parser with memory caching and custom suffixes $parser2 = new DomainParser(true, ['com.internal']); $host1 = 'sub2.sub1.example.com'; $host2 = 'sub2.sub1.example.com.internal'; // Parse host1 $parser->showResult([ 'tld' => $parser->tld($host1), // Output: "com" 'domain' => $parser->getRegistrableDomain($host1), // Output: "example.com" 'subdomain' => $parser->getSubdomain($host1), // Output: "sub2" 'subdomains' => $parser->getSubdomains($host1), // Output: ["sub2", "sub1"] ]); // Parse host2 $parser->showResult([ 'tld' => $parser->tld($host2), // Output: "internal" 'domain' => $parser->getRegistrableDomain($host2), // Output: "example.com.internal" 'subdomain' => $parser->getSubdomain($host2), // Output: "sub2" 'subdomains' => $parser->getSubdomains($host2), // Output: ["sub2", "sub1"] ]); // Parse host2 with custom suffixes $parser2->showResult([ 'tld' => $parser2->tld($host2), // Output: "com.internal" 'domain' => $parser2->getRegistrableDomain($host2), // Output: "example.com.internal" 'subdomain' => $parser2->getSubdomain($host2), // Output: "sub2" 'subdomains' => $parser2->getSubdomains($host2), // Output: ["sub2", "sub1"] ]);
This design offers simplicity and flexibility for validation, user registration, email domain verification, and other domain-related tasks.
Caching and Updates
The suffix list is retrieved from publicsuffix.org and cached locally. The parser automatically refreshes this cache every three days to maintain currency. In performance-critical or long-running environments, enabling in-memory caching avoids repeated disk access during runtime.
Enable memory caching as follows:
$parser = new DomainParser(memoryCache: true);
Framework Integration Potential
Owing to its modular design and PSR-4 compliance, SilvarCode Domain Parser integrates smoothly with major PHP frameworks:
- CakePHP: Utility class or validation rule
- Laravel: Service, validation rule, or middleware
- Symfony: Validator or dependency-injected service
- Laminas / Mezzio: Standalone utility or filter
Its zero-dependency architecture also makes it ideal for CLI scripts, CMS plugins, and headless applications.
Final Thoughts
Domain parsing is a critical function in many security-sensitive and tenant-aware applications. SilvarCode Domain Parser offers a transparent, efficient, and standards-aligned solution for managing domain complexities in PHP.
If this library benefits your project, please consider starring the GitHub repository, contributing improvements, or sharing it with your network.