note 72274 added to function.strlen
| From: | paolo dot mosna at gmail dot com | Date: | Wed, 10 Jan 2007 15:58:50 +0000 |
| Subject: | note 72274 added to function.strlen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121537@lists.php.net to get a copy of this message | ||
Title: Strlen() ant bytes string lenght.
Just to remember that strlen() return the number of characters of a string. Often the strlen()
function is used to compute the length in bytes of a string. This is correct until string is single
byte encoded. If multi-byte char-set is used this constraint i no more verified. So when you require
the number of bytes of a ASCII or UTF-8 encoded string, it is better to use following function:
/**
* 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 < $strlen_var; ++$c) {
$ord_var_c = ord($str{$d});
switch (true) {
case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
// characters U-00000000 - U-0000007F (same as ASCII)
$d++;
break;
case (($ord_var_c & 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 & 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 & 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 & 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 & 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;
}
This function has been adapted form the JSON function used to convert character in UTF-8
representation.
With this new function we solved problem in JSON and in PEAR/SOAP php libraries.
----
Server IP: 213.92.11.19
Probable Submitter: 62.77.56.16 (proxied: 1.1 GRFPRX006RM001)
----
Manual Page -- http://www.php.net/manual/en/function.strlen.php
Edit -- https://master.php.net/note/edit/72274
Del: integrated -- https://master.php.net/note/delete/72274/integrated
Del: useless -- https://master.php.net/note/delete/72274/useless
Del: bad code -- https://master.php.net/note/delete/72274/bad+code
Del: spam -- https://master.php.net/note/delete/72274/spam
Del: non-english -- https://master.php.net/note/delete/72274/non-english
Del: in docs -- https://master.php.net/note/delete/72274/in+docs
Del: other reasons-- https://master.php.net/note/delete/72274
Reject -- https://master.php.net/note/reject/72274
Search -- https://master.php.net/manage/user-notes.php