PHP code example of hampel / tlds

1. Go to this page and download the library: Download hampel/tlds library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hampel / tlds example snippets


// get a "fresh" copy of the TLD list
$tld_array = Tlds::fresh();

// or if you prefer to not use Facades:
$tld_array = $app->make(Hampel\Tlds\TldManager::class)->fresh();

// get the TLD list from the cache (or update the cache if it has expired)
$tld_array = Tlds::get();

use Hampel\Tlds\Fetcher\TldFetcher;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->bind(TldFetcher::class, MyTldFetcher::class);
    }
}

use Hampel\Tlds\Exceptions\FetchException;
use Hampel\Tlds\Fetcher\TldFetcher;

class MyTldFetcher implements TldFetcher
{
    public function fetchTlds()
    {
        // throw a FetchException, or a subclass of it, if the data cannot be retrieved
        return file_get_contents('/path/to/tlds-alpha-by-domain.txt');
    }
}

use Hampel\Tlds\Validation\Domain;

// valid values: example.com; example.au
// invalid values: example.invalid-tld

$request->validate([
    'domain' => ['

use Hampel\Tlds\Validation\DomainIn;

// valid values: example.com; example.net
// invalid values: example.co; example.au (not in specified list of domain TLDs)

$request->validate([
    'domain' => ['

use Hampel\Tlds\Validation\Tld;

// valid values: example.com; com; net (either a domain with a valid TLD, or a string that is itself a valid TLD)
// invalid values: example.invalid-tld; something-not-a-tld

$request->validate([
    'domain' => ['

use Hampel\Tlds\Validation\TldIn;

// valid values: example.com; example.net; com; net
// invalid values: example.co; org (not in specified list of domain TLDs)

$request->validate([
    'domain' => ['
bash
$ php artisan vendor:publish --provider="Hampel\Tlds\TldServiceProvider"