This is a discussion on binary string length within the PHP Language forums, part of the PHP Programming Forums category; Hi strlen does not return the correct value .I compared the filesize() and strlen byte size but they are not ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
gezerpunta wrote:
> 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() 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 compleja. |
|
|||
|
On May 28, 10:40 am, gezerpunta <css...@gmail.com> wrote:
> Hi > > 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() > > thks. http://us.php.net/manual/en/function...rlen.php#72979 Google searches ("php binary string length") come in handy... |
|
|||
|
On May 28, 6:02 pm, "farri...@gmail.com" <farri...@gmail.com> wrote:
> On May 28, 10:40 am, gezerpunta <css...@gmail.com> wrote: > > > Hi > > > 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() > > > thks. > > http://us.php.net/manual/en/function...rlen.php#72979 > > Google searches ("php binary string length") come in handy... In PHP, like in C, the string ends with a zero-character, '\0', (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. |
|
|||
|
On 28 May 2007 09:29:46 -0700, Darko <darko.maksimovic@gmail.com> wrote:
>On May 28, 6:02 pm, "farri...@gmail.com" <farri...@gmail.com> wrote: >> On May 28, 10:40 am, gezerpunta <css...@gmail.com> wrote: >> >> > 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() >> >> http://us.php.net/manual/en/function...rlen.php#72979 >> >> Google searches ("php binary string length") come in handy... > >In PHP, like in C, the string ends with a zero-character, '\0', (char) >0, null-terminator, null-byte or whatever you like to call 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 <?php $x = chr(0) . chr(0) . "blah" . chr(0) . chr(0); print strlen($x); ?> $ php test.php 8 -- Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool |
|
|||
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 Andy Hassall wrote: > 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. [snip] 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 <htmlpurifier.org> Anti-XSS HTML Filter [[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]] -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGWytBqTO+fYacSNoRAgcBAJ9FrhQ62Z6HVRFBgOKwiX IiciAZjACfXhL4 A9uNj8G02gyQOOpp1Q/vVhs= =xbaE -----END PGP SIGNATURE----- |