You are here: Re: binary string length « PHP Programming Language « IT news, forums, messages
Re: binary string length

Posted by Andy Hassall on 05/28/07 17:53

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация