- mb_convert_encoding: documentation ( source)
- substr_count: documentation ( source)
- preg_replace_callback: documentation ( source)
- print_r: documentation ( source)
- pack: documentation ( source)
- preg_replace: documentation ( source)
<?php
class DomainName
{
private $name;
private $escapedName;
public function __construct($name)
{
$decodeUnicodeName = static::decodeUnicode($name);
$this->name = $decodeUnicodeName;
$this->escapedName = static::escapeDomainName($decodeUnicodeName);
}
public function containsSubdomain()
{
return substr_count($this->name, '.') >= 2;
}
public static function escapeDomainName($name)
{
return preg_replace('/\./', '-', $name);
}
public static function decodeUnicode(string $name): string
{
return preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}, $name);
}
public function __toString(): string
{
return (string)$this->name;
}
}
$name = 'apotheke-altenessen-s\u00fcd.de';
$test = new DomainName($name);
print_r((string)$test);