tags:

views:

37

answers:

2

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

+1  A: 

On php.org, someone was nice enough to create this function. Just multiply by 8 and you've got however many bits were in that string, as the function returns bytes.

31eee384
So strlen should effectively work fine for strings holding random binary data?
Greg
I don't think strlen gives you anything but the number of characters, which is why I linked to that method.But even that method looks like it's designed to be used on a string that contains characters.
31eee384
+1  A: 

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

You don't check the length of binary data. You have to know it beforehand. In your case, the length is 16 (bytes, not bits, if it is UUID).

As far as UUID validity is concerned, any 16-byte value is a valid UUID, so you are out of luck there.

zvonimir
Apologies, I meant to say bytes. Thank you for explaining.
Greg