title: How can I get the single bytes from a multibyte PHP string variable in a binary-safe way?
body: <p>Let's say (for simplicity's sake) that I have a multibyte, UTF-8 encoded string variable with 3 letters (consisting of 4 bytes):</p>
  
  <pre><code>$original = 'Fön';
  </code></pre>
  
  <p>Since it's UTF-8, the bytes' hex values are (excluding the BOM):</p>
  
  <pre><code>46 C3 B6 6E
  </code></pre>
  
  <p>As the <code>$original</code> variable is user-defined, I will need to hande two things:</p>
  
  <ol>
  <li>Get the exact number of bytes (not UTF-8 characters) used in the string, and</li>
  <li>A way to access each individual byte (not UTF-8 character).</li>
  </ol>
  
  <p>I would tend to use <code>strlen()</code> to handle "1.", and access the <code>$original</code> variable's bytes with a simple <code>`$original[$byteposition]</code> like this:</p>
  
  <pre><code>&lt;?php
  header('Content-Type: text/html; charset=UTF-8');
  
  $original = 'Fön';
  $totalbytes = strlen($original);
  for($byteposition = 0; $byteposition &lt; $totalbytes; $byteposition++)
  {
      $currentbyte = $original[$byteposition];
  
      /*
          Doesn't work since var_dump shows 3 bytes.
      */
      var_dump($currentbyte);
  
      /*
          Fails too since "ord" only works on ASCII chars.
          It returns "46 F6 6E"
      */
      printf("%02X", ord($currentbyte));
      echo('&lt;br&gt;');
  }
  
  exit();
  ?&gt;
  </code></pre>
  
  <p>This proves my initial idea is not working:</p>
  
  <ol>
  <li>var_dump shows 3 bytes</li>
  <li>printf fails too since "ord" only works on ASCII chars</li>
  </ol>
  
  <p>How can I get the single bytes from a multibyte PHP string variable in a binary-safe way?</p>
  
  <p>What I am looking for is a binary-safe way to convert UTF-8 string(s) into byte-array(s).</p>