RsD0p9BK
4/18/2017 - 11:35 AM

toCodePoint.php

/**
 * Convert a string into an array of decimal Unicode code points.
 *
 * @param $string   [string] The string to convert to codepoints
 * @param $encoding [string] The encoding of $string
 * 
 * @return [array] Array of decimal codepoints for every character of $string
 */
function toCodePoint( $string, $encoding ) {
  $utf32  = mb_convert_encoding( $string, 'UTF-32', $encoding );
  $length = mb_strlen( $utf32, 'UTF-32' );
  $result = [];

  for( $i = 0; $i < $length; ++$i ) {
    $result[] = hexdec( bin2hex( mb_substr( $utf32, $i, 1, 'UTF-32' ) ) );
  }
  
  return $result;
}

// http://stackoverflow.com/questions/7106470/utf-8-to-unicode-code-points