Viewed   81 times

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

Monday, October 31, 2022


$num = 245354;
$numlength = strlen((string)$num);
Wednesday, November 16, 2022

You are searching for .=:

$str = "";
$str .= "some value";
Tuesday, November 22, 2022

No, it is not always x2 for UTF-8 and changes based on the actual contents. For ASCII characters it is 1 byte, but can go into several bytes for large code-point values. You want:

string s = // your string
int len = Encoding.UTF8.GetByteCount(s);
Sunday, October 9, 2022

Your code is right, the below line will give you the size in bytes:


You can also get the file size after the file has been uploaded this way:

echo filesize($perDestination) . ' bytes';  

This option will also give you the file size in bytes

Tuesday, August 30, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :