PHP code example of cybercog / php-unicode
1. Go to this page and download the library: Download cybercog/php-unicode 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/ */
cybercog / php-unicode example snippets
$codePoint = \Cog\Unicode\CodePoint::of('ÿ');
$codePoint = \Cog\Unicode\CodePoint::ofDecimal(255);
$codePoint = \Cog\Unicode\CodePoint::ofHexadecimal('U+00FF');
$codePoint = \Cog\Unicode\CodePoint::ofHtmlEntity('ÿ');
$codePoint = \Cog\Unicode\CodePoint::ofXmlEntity('ÿ');
$codePoint = \Cog\Unicode\CodePoint::of('ÿ');
echo strval($codePoint); // (string) "ÿ"
echo $codePoint->toDecimal(); // (int) 255
echo $codePoint->toHexadecimal(); // (string) "U+00FF"
echo $codePoint->toHtmlEntity(); // (string) "ÿ"
echo $codePoint->toXmlEntity(); // (string) "ÿ"
$string = \Cog\Unicode\UnicodeString::of('Hello');
echo strval($string); // (string) "Hello"
$codePointList = $string->codePointList; // list<CodePoint>
$grapheme = \Cog\Unicode\Grapheme::of('๐จ๐ฉ๐ง๐ฆ');
echo strval($grapheme); // (string) "๐จ๐ฉ๐ง๐ฆ"
$codePointList = $grapheme->codePointList; // list<CodePoint>
$string = \Cog\Unicode\GraphemeString::of('ะะต๐จ๐ฉ๐ง๐ฆ');
$graphemeList = $string->graphemeList; // list<Grapheme>
// 'ะ', 'ะต', '๐จ๐ฉ๐ง๐ฆ' — 3 graphemes (not 9 code points)
echo strval($string); // (string) "ะะต๐จ๐ฉ๐ง๐ฆ"
$codePoint = \Cog\Unicode\CodePoint::of('©');
echo $codePoint->toDecimal(); // 169
echo $codePoint->toHexadecimal(); // "U+00A9"
echo $codePoint->toHtmlEntity(); // "©"
echo $codePoint->toXmlEntity(); // "©"
$cp = \Cog\Unicode\CodePoint::ofHtmlEntity('♥');
echo $cp->toXmlEntity(); // "♥"
echo $cp->toDecimal(); // 9829
$cp2 = \Cog\Unicode\CodePoint::ofDecimal($cp->toDecimal());
echo strval($cp2); // "♥"
$string = \Cog\Unicode\UnicodeString::of('café');
foreach ($string->codePointList as $cp) {
echo $cp->toHexadecimal() . ' ';
}
// U+0063 U+0061 U+0066 U+00E9
// Flag emoji: 2 code points, but 1 visible character
$flag = \Cog\Unicode\UnicodeString::of('๐ฆ๐ถ');
echo count($flag->codePointList); // 2
$flag = \Cog\Unicode\GraphemeString::of('๐ฆ๐ถ');
echo count($flag->graphemeList); // 1
// Family emoji: 7 code points (persons + ZWJ), 1 visible character
$family = \Cog\Unicode\GraphemeString::of('๐จ๐ฉ๐ง๐ฆ');
echo count($family->graphemeList); // 1
$familyGrapheme = $family->graphemeList[0];
echo count($familyGrapheme->codePointList); // 7
$acute = \Cog\Unicode\CodePoint::of("\u{0301}"); // combining acute accent
echo $acute->isCombining(); // true
$a = \Cog\Unicode\CodePoint::of('A');
echo $a->isCombining(); // false
// Procedural
$char = '©';
$dec = mb_ord($char);
$hex = 'U+' . strtoupper(sprintf('%04X', $dec));
$html = htmlentities($char, ENT_HTML5 | ENT_QUOTES);
// With this library
$cp = \Cog\Unicode\CodePoint::of('©');
$dec = $cp->toDecimal();
$hex = $cp->toHexadecimal();
$html = $cp->toHtmlEntity();
shell
composer