I am doing a real estate feed for a portal and it is telling me the max length of a string should be 20,000 bytes (20kb), but I have never run across this before.
How can I measure byte
size of a varchar string
. So I can then do a while loop to trim it down.
You can use mb_strlen() to get the byte length using a encoding that only have byte-characters, without worring about multibyte or singlebyte strings. For example, as drake127 saids in a comment of mb_strlen, you can use '8bit' encoding:
You can have problems using strlen function since php have an option to overload strlen to actually call mb_strlen. See more info about it in http://php.net/manual/en/mbstring.overload.php
For trim the string by byte length without split in middle of a multibyte character you can use:
PHP's
strlen()
function returns the number of ASCII characters.strlen('borsc')
-> 5 (bytes)strlen('boršč')
-> 7 (bytes).. or you can use a function like this:
Further to PhoneixS answer to get the correct length of string in bytes - Since
mb_strlen()
is slower thanstrlen()
, for the best performance one can check "mbstring.func_overload" ini setting so thatmb_strlen()
is used only when it is really required:Do you mean byte size or string length?
Byte size is measured with
strlen()
, whereas string length is queried usingmb_strlen()
. You can usesubstr()
to trim a string to X bytes (note that this will break the string if it has a multi-byte encoding - as pointed out by Darhazer in the comments) andmb_substr()
to trim it to X characters in the encoding of the string.You have to figure out if the string is ascii encoded or encoded with a multi-byte format.
In the former case, you can just use
strlen
.In the latter case you need to find the number of bytes per character.
the strlen documentation gives an example of how to do it : http://www.php.net/manual/en/function.strlen.php#72274