Check binary string length?

Is there a native or inexpensive way to check string length in bytes in PHP?

+5
source share
4 answers

The length of the string (text data) is determined by the position of the NULL character, which indicates the end. In the case of binary data, NULL can be and often is in the middle of the data.

You do not check the length of the binary data. You must know this beforehand. In your case, the length is 16 (bytes, not bits if it's a UUID).

Regarding the validity of the UUID, any 16-byte value is a valid UUID, so you're out of luck there.

-4
source

See http://bytes.com/topic/php/answers/653733-binary-string-length

Relevant Part:

" PHP, C, , '\ 0', (char)   0, -, - , .

, - PHP , , C, . " - "- NUL .

. zvalue_value zend.h; " char* val "" int len ​​".

, mbstring.func_overload, strlen() , , . PHP.

, strlen . , . , mbstring, strlen.

+16

php.org - , . 8, , , , .

+2

​​ mbstring , , :

$len=strlen(bin2hex($data))/2;

, Hex 2 , bin2hex ( 0).

Please note that it will use significantly more resources than regular strlen(afterall, so you do not have to do this with a large amount of data if it is not necessary.

+2
source

All Articles