How in php can I check if a string is larger than 2 MB? We need to truncate strings that are large before inserting them into MongoDB, since the MongoDB document limit is 16MB.
UPDATE
The string can indeed have non ascii characters, i.e. utf.
You can use:
<?php
if (mb_strlen($string, '8bit') > 1024 * 1024 * 2) {
echo "larger that 2 MB";
}
?>
You can't.
A string is a sequence of characters. The size of each character depends on what character it is and how it is encoded once saved to a file.
So a string itself has no size except the internal memory allocations for your PHP interpreter.
If performance is not an issue, you could write the string to a file with a character encoding of your choice and then examine the size of the written file.