PHP code example of rowbot / idna

1. Go to this page and download the library: Download rowbot/idna 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/ */

    

rowbot / idna example snippets


  use Rowbot\Idna\Idna;

  $result = Idna::toAscii('x-.xn--nxa');

  // You must not use an ASCII domain that has errors.
  if ($result->hasErrors()) {
      throw new \Exception();
  }

  echo $result->getDomain(); // x-.xn--nxa
  

  use Rowbot\Idna\Idna;

  $result = Idna::toUnicode('xn---with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n.com');
  echo $result->getDomain(); // 安室奈美恵-with-super-monkeys.com
  

  // Default options.
  [
    'CheckHyphens'            => true,
    'CheckBidi'               => true,
    'CheckJoiners'            => true,
    'UseSTD3ASCIIRules'       => true,
    'Transitional_Processing' => false,
    'VerifyDnsLength'         => true, // Only for Idna::toAscii()
  ];
  

  use Rowbot\Idna\Idna;

  $result = Idna::toAscii('x-.xn--nxa', ['CheckHyphens' => true]);
  $result->hasErrors(); // true
  $result->hasError(Idna::ERROR_TRAILING_HYPHEN); // true

  $result = Idna::toAscii('x-.xn--nxa', ['CheckHyphens' => false]);
  $result->hasErrors(); // false
  $result->hasError(Idna::ERROR_TRAILING_HYPHEN); // false
  

  $input = '憡?Ⴔ.XN--1UG73GL146A';

  idn_to_utf8($input, 0, IDNA_INTL_VARIANT_UTS46, $info);
  echo $info['result']; // 憡��.xn--1ug73gl146a�
  echo ($info['errors'] & IDNA_ERROR_DISALLOWED) !== 0; // true

  $result = \Rowbot\Idna\Idna::toUnicode($input);
  echo $result->getDomain(); // 憡?Ⴔ.xn--1ug73gl146a
  echo $result->hasError(\Rowbot\Idna\Idna::ERROR_DISALLOWED); // true
  
bash
php bin/generateDataFiles.php