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.

 Answers

4

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

Monday, October 31, 2022
4

Maybe:

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

You are searching for .=:

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

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
 
ed_kirk
 
2

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

size=$_FILES['image']['size'];

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 :