PHP code example of nick-jones / php-ucd
1. Go to this page and download the library: Download nick-jones/php-ucd 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/ */
nick-jones / php-ucd example snippets
use UCD\Unicode\Character;
use UCD\Unicode\Character\Properties\General\Block;
use UCD\Database;
$filter = function (Character $character) {
$properties = $character->getProperties();
$general = $properties->getGeneral();
$block = $general->getBlock();
return $properties->isNumeric()
&& !$block->equals(Block::fromValue(Block::BASIC_LATIN));
};
$dumper = function (Character $character) {
$codepoint = $character->getCodepoint();
$properties = $character->getProperties();
$numerity = $properties->getNumericity();
$number = $numerity->getNumber();
$utf8 = $codepoint->toUTF8();
printf("%s: %s (~ %s)\n", $codepoint, $utf8, $number);
};
Database::fromDisk()
->onlyCharacters()
->filterWith($filter)
->traverseWith($dumper);
// outputting:
// U+B2: ² (~ 2)
// U+B3: ³ (~ 3)
// U+B9: ¹ (~ 1)
// U+BC: ¼ (~ 1/4)
// U+BD: ½ (~ 1/2)
// U+BE: ¾ (~ 3/4)
// U+660: ٠ (~ 0)
// U+661: ١ (~ 1)
// U+662: ٢ (~ 2)
// U+663: ٣ (~ 3)
// <snip>
use UCD\Database;
use UCD\Unicode\Codepoint;
$database = Database::fromDisk();
$codepoint = Codepoint::fromInt(9731);
// ..or $codepoint = Codepoint::fromHex('2603');
// ..or $codepoint = Codepoint::fromUTF8('☃');
$character = $collection->getCharacterByCodepoint($codepoint);
$properties = $character->getProperties();
$general = $properties->getGeneral();
$names = $general->getNames();
// prints "U+2603: SNOWMAN"
printf("%s: %s\n", $character->getCodepoint(), $names->getPrimary());
use UCD\Database;
use UCD\Unicode\Codepoint;
$database = Database::fromDisk();
$string = 'abc';
$codepoints = Codepoint\Collection::fromUTF8($string);
$assigned = $database->getByCodepoints($codepoints);
foreach ($assigned->getCharacters() as $character) {
$properties = $character->getProperties();
$general = $properties->getGeneral();
$names = $general->getNames();
printf("%s: %s\n", $character->getCodepoint(), $names->getPrimary());
}
// outputting:
// U+61: LATIN SMALL LETTER A
// U+62: LATIN SMALL LETTER B
// U+63: LATIN SMALL LETTER C
use UCD\Database;
use UCD\Unicode\Character;
use UCD\Unicode\Character\Properties\General\Block;
$filter = function (Character $character) {
$properties = $character->getProperties();
$general = $properties->getGeneral();
$block = $general->getBlock();
return $properties->isNumeric()
&& $block->equals(Block::fromValue(Block::BENGALI));
};
$cc = Database::fromDisk()
->onlyCharacters()
->filterWith($filter)
->extractCodepoints()
->aggregate()
->toRegexCharacterClass();
$regex = sprintf('/^%s$/u', $cc);
var_dump($regex); // string(37) "/^[\x{9E6}-\x{9EF}\x{9F4}-\x{9F9}]$/u"
var_dump(preg_match($regex, '১')); // int(1)
var_dump(preg_match($regex, '1')); // int(0)
use UCD\Database;
$characters = Database::fromDisk()
->onlyCharacters();
echo 'static $map = [' . PHP_EOL;
foreach ($characters as $character) {
$codepoint = $character->getCodepoint();
$properties = $character->getProperties();
$case = $properties->getLetterCase();
$mappings = $case->getMappings();
$upperMapping = $mappings->getUppercase();
$upper = $upperMapping->getSimple();
if (!$upper->equals($codepoint)) {
$from = $codepoint->toUnicodeEscape();
$to = $upper->toUnicodeEscape();
printf(' "%s" => "%s",', $from, $to);
echo PHP_EOL;
}
}
echo '];';
// outputting:
// static $map = [
// "\u{61}" => "\u{41}",
// "\u{62}" => "\u{42}",
// "\u{63}" => "\u{43}",
// <snip>
// "\u{1E942}" => "\u{1E920}",
// "\u{1E943}" => "\u{1E921}",
// ];
$lower = 'aς!';
$upper = '';
for ($i = 0; $i < mb_strlen($lower); $i++) {
$char = mb_substr($lower, $i, 1);
$upper .= $map[$char] ?? $char;
}
var_dump($upper); // string(4) "AΣ!"