title: How do I find the number of bytes within UTF-8 string with PHP?
body: <p>I have the following function from the php.net site to determine the # of bytes in an ASCII and UTF-8 string:</p>
  
  <pre><code>&lt;?php 
  /** 
   * Count the number of bytes of a given string. 
   * Input string is expected to be ASCII or UTF-8 encoded. 
   * Warning: the function doesn't return the number of chars 
   * in the string, but the number of bytes. 
   * 
   * @param string $str The string to compute number of bytes 
   * 
   * @return The length in bytes of the given string. 
   */ 
  function strBytes($str) 
  { 
    // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT 
  
    // Number of characters in string 
    $strlen_var = strlen($str); 
  
    // string bytes counter 
    $d = 0; 
  
   /* 
    * Iterate over every character in the string, 
    * escaping with a slash or encoding to UTF-8 where necessary 
    */ 
    for ($c = 0; $c &lt; $strlen_var; ++$c) { 
  
        $ord_var_c = ord($str{$d}); 
  
        switch (true) { 
            case (($ord_var_c &gt;= 0x20) &amp;&amp; ($ord_var_c &lt;= 0x7F)): 
                // characters U-00000000 - U-0000007F (same as ASCII) 
                $d++; 
                break; 
  
            case (($ord_var_c &amp; 0xE0) == 0xC0): 
                // characters U-00000080 - U-000007FF, mask 110XXXXX 
                // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
                $d+=2; 
                break; 
  
            case (($ord_var_c &amp; 0xF0) == 0xE0): 
                // characters U-00000800 - U-0000FFFF, mask 1110XXXX 
                // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
                $d+=3; 
                break; 
  
            case (($ord_var_c &amp; 0xF8) == 0xF0): 
                // characters U-00010000 - U-001FFFFF, mask 11110XXX 
                // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
                $d+=4; 
                break; 
  
            case (($ord_var_c &amp; 0xFC) == 0xF8): 
                // characters U-00200000 - U-03FFFFFF, mask 111110XX 
                // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
                $d+=5; 
                break; 
  
            case (($ord_var_c &amp; 0xFE) == 0xFC): 
                // characters U-04000000 - U-7FFFFFFF, mask 1111110X 
                // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
                $d+=6; 
                break; 
            default: 
              $d++;    
        } 
    } 
  
    return $d; 
  } 
  ?&gt; 
  </code></pre>
  
  <p>However when I try this with Russian (e.g. <code>По своей природе компьютеры могут работать лишь с числами. И для того, чтобы они могли хранить в памяти буквы или другие символы, каждому такому символу должно быть поставлено в соответствие число.</code>). It doesn't seem to return the correct number of bytes.</p>
  
  <p>The switch statement is using the default condition.  Any ideas why Russian characters would not be working as expected?  Or would there be better options for this.</p>
  
  <p>I am asking this as I need to shorten a UTF-8 string to a certain number of bytes.  i.e. I can only send a max. of 169 bytes of JSON data to the iPhone APNS in my situation (excluding the other packet data).</p>
  
  <p>Reference: <a href="http://php.net/manual/en/function.strlen.php" rel="noreferrer">PHP strlen - Manual (Paolo Comment on 10-Jan-2007 03:58)</a></p>