title: JS charCodeAt equivalent in PHP (with full unicode and emoji compatibility)
body: <p>I have a simple code in JS that I can't replicate in PHP if it comes to special characters.</p>
  
  <p>This is the JS code (see <a href="https://jsfiddle.net/h8oca3qg/5/" rel="noreferrer">JSFiddle</a> for output):</p>
  
  <pre><code>var str = "t↙️"; //char "t" and special characters, emojis, etc..
  document.write("Length is: "+str.length); // Length is: 19
  for(var i=0; i&lt;str.length; i++) {
    document.write("&lt;br&gt; charCodeAt(" + i + "): " + str.charCodeAt(i));
  }
  </code></pre>
  
  <p>The first problem is that PHP <code>strlen()</code> and <code>mb_strlen()</code> already gives different results from JS <em>(strlen: 39, mb_strlen: 11)</em>, however I managed to get the same with a custom <code>JS_StringLength</code> function (thanks to <a href="https://stackoverflow.com/a/30607540/2953830">this</a> SO answer). </p>
  
  <p>Here is what I have in PHP so far (see <a href="http://phpfiddle.org/main/code/simd-0rq3" rel="noreferrer">phpFiddle</a> for output):</p>
  
  <pre><code>&lt;?php
  
  function JS_StringLength($string) {
      return strlen(iconv('UTF-8', 'UTF-16LE', $string)) / 2;
  }
  
  function JS_charCodeAt($str, $index){
      //not working!
  
      $char = mb_substr($str, $index, 1, 'UTF-8');
      if (mb_check_encoding($char, 'UTF-8'))
      {
          $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
          return hexdec(bin2hex($ret));
      } else {
          return null;
      }
  }
  
  $str = "t↙️";
  
  echo $str."\n";
  //echo "Length is: ".strlen($str)."\n"; //wrong
  echo "Length is: ".JS_StringLength($str)."\n"; //OK
  for($i=0; $i&lt;JS_StringLength($str); $i++) {
      echo "charCodeAt(".$i."): ".JS_charCodeAt($str, $i)."\n";
  }
  </code></pre>
  
  <p>After a full day of Googling, <a href="https://stackoverflow.com/questions/31802180/how-to-convert-javascript-to-php">and</a> <a href="https://stackoverflow.com/q/10333098/2953830">trying</a> <a href="https://stackoverflow.com/q/12990195/2953830">out</a> <a href="http://web.archive.org/web/20170424043707/eqcode.com/wiki/CharCodeAt" rel="noreferrer">everything</a> I found, nothing gave the same results as JS. 
  What should <code>JS_charCodeAt</code> be to get the <em>same output</em> as JS with <em>similar performance</em>?</p>
  
  <p>Experimenting #1:<br>
  Enter my string into <a href="https://r12a.github.io/app-conversion/" rel="noreferrer">https://r12a.github.io/app-conversion/</a> (awesome stuff). Looks like JS works with <em>UTF-16 code units</em> (19) and PHP <code>strlen</code> counts <em>UTF-8 code units</em> (39). </p>
  
  <p>Experimenting #2:<br>
  When using <code>json_encode()</code> on my string - of course - the result will almost be something like that, what JavaScript may uses. I even examined the original PHP source code of json_encode and how <a href="https://github.com/php/php-src/blob/c1ce43d1d2e500aeb0143c7c0f051e40f09ce4db/ext/json/json_encoder.c#L252-L469" rel="noreferrer">json_encode escapes strings</a>, but.. well..</p>
  
  <hr>
  
  <p><em>Before flagging as a duplicate, please make sure you test a solution with the string in the above examples (or random emojis) as ALL the charCodeAt implementations found here on stackoverflow are working with most of the special characters, but NOT with emojis.</em></p>