Advertisement
Guest User

Untitled

a guest
Nov 28th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.76 KB | None | 0 0
  1. <?php
  2.  
  3. function getUTF16CodeUnits($string) {
  4.     $string = substr(json_encode($string), 1, -1);
  5.     preg_match_all("/\\\\u[0-9a-fA-F]{4}|./mi", $string, $matches);
  6.     return $matches[0];
  7. }
  8.  
  9. function JS_charCodeAt($utf16CodeUnitsArray, $index) {
  10.     $unit = $utf16CodeUnitsArray[$index];
  11.    
  12.     if(strlen($unit) > 1) {
  13.         $hex = substr($unit, 2);
  14.         return hexdec($hex);
  15.     }
  16.     else {
  17.         return ord($unit);
  18.     }
  19. }
  20.  
  21. $str = "tπŸ™πŸΏπŸ˜˜πŸŽšβ†™οΈπŸ•—πŸ‡¨πŸ‡¬π―¦”";
  22. $str = str_repeat($str, 250); //repeat $str 250 times
  23.  
  24. $utf16CodeUnits = getUTF16CodeUnits($str);
  25.  
  26. echo "Length is: ".count($utf16CodeUnits)."\n";
  27. for($i=0; $i<count($utf16CodeUnits); $i++) {
  28.     echo "charCodeAt(".$i."): ".JS_charCodeAt($utf16CodeUnits, $i)."\n";
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement