Skip to content

Instantly share code, notes, and snippets.

@qazd
Last active July 9, 2018 16:49
Show Gist options
  • Save qazd/6172f35c8a2452e8077c06a566895963 to your computer and use it in GitHub Desktop.
Save qazd/6172f35c8a2452e8077c06a566895963 to your computer and use it in GitHub Desktop.
Converts each character of a text string to its hexadecimal representation from the Unicode code table. Forms a sequence delimited by space. Speed and efficiency were not tested.
<?php
$string = 'è Ё𐐷-Xπ 🤔';
foreach (preg_split('//u', $string) as $char) {
if (!$char) continue;
$hex = base_convert(mb_ord($char, 'UTF-8'), 10, 16);
$pad = strlen($hex) > 4 ? 8 : 4;
$hex = str_pad($hex, $pad, '0', STR_PAD_LEFT);
echo 'U+' . $hex . ' ';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment