binary string length

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-28-2007
gezerpunta
 
Posts: n/a
Default binary string length

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.

Reply With Quote
  #2 (permalink)  
Old 05-28-2007
Iván Sánchez Ortega
 
Posts: n/a
Default Re: binary string length

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.
Reply With Quote
  #3 (permalink)  
Old 05-28-2007
farrishj@gmail.com
 
Posts: n/a
Default Re: binary string length

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...

Reply With Quote
  #4 (permalink)  
Old 05-28-2007
Darko
 
Posts: n/a
Default Re: binary string length

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.

Reply With Quote
  #5 (permalink)  
Old 05-28-2007
Andy Hassall
 
Posts: n/a
Default Re: binary string length

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
Reply With Quote
  #6 (permalink)  
Old 05-28-2007
Edward Z. Yang
 
Posts: n/a
Default Re: binary string length

-----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-----
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:11 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0