binary string length

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

strlen does not return the correct value .I compared the filesize()
and strlen byte size but they are not equal. I must find binary string
length and it must be equal to filesize()


Re: binary string length

gezerpunta wrote:

Quoted text here. Click to load it

Then use filesize(). D'oh!

Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Un ordenador no es un televisor ni un microondas, es una herramienta

Re: binary string length

Quoted text here. Click to load it

Google searches ("php binary string length") come in handy...

Re: binary string length

Quoted text here. Click to load it

In PHP, like in C, the string ends with a zero-character, '', (char)
0, null-terminator, null-byte or whatever you like to call it. Thus,
if you have binary strings, they probably have this character
somewhere inside them, although that doesn't mean it's the end of the
string. From this reason, it's not very smart to use ordinary
strlen(), and str* functions in general, for binary data. Use, as
farrishj suggested, mb-strlen (multibyte string), or use filesize()
directly, as Ortega suggested. You can, however, make your own
functions to take care of this, just remember the number of bytes you
read together with the data - this, of course, if aforementioned
functions are not good enough for you.

Re: binary string length

Quoted text here. Click to load it

 No, that's not the case - PHP strings are stored with both the length and the
data, unlike C strings that just has one pointer and uses a terminator. They're
"binary-safe" - NUL doesn't terminate the string.

 See the definition of zvalue_value in zend.h; the string part has both a "char
*val" and "int len".

 Problems would start if you're using the mbstring.func_overload, which changes
how strlen() and the other functions work, and does try and treat strings as
strings of characters in a specific encoding rather than a string of bytes.
This is not the normal PHP behaviour.

$ cat test.php  
$x = chr(0) . chr(0) . "blah" . chr(0) . chr(0);
print strlen($x);

$ php test.php  

Andy Hassall :: :: :: disk and FTP usage analysis tool

Re: binary string length

Hash: SHA1

Andy Hassall wrote:
Quoted text here. Click to load it

Andy is correct. By default, PHP strings are treated like binary
strings, which works great for 8-bit encodings but not so much for UTF-8
or other multibyte character encodings. mbstring tries to "fix" this
using the automatic string overload, but then you lose the ability to
process binary data.

- --
 Edward Z. Yang                        GnuPG: 0x869C48DA
 HTML Purifier  <>  Anti-XSS HTML Filter
 [[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla -


Site Timeline