When checking for length to make sure a value will fit in a database field, be mindful of using the right function.
There are three possible situations:
1. Most likely case: the database column is UTF-8 with a length defined in unicode code points (e.g. mysql varchar(200) for a utf-8 database).
<?php
mb_strlen($value);
iconv_strlen($value);
mb_strlen($value, "UTF-8");
iconv_strlen($value, "UTF-8");
strlen(utf8_decode($value)); grapheme_strlen($value); ?>
2. The database column has a length defined in bytes (e.g. oracle's VARCHAR2(200 BYTE))
<?php
strlen($value);
mb_strlen($value, "8bit")
?>
3. The database column is in another character set (UTF-16, ISO-8859-1, etc...) with a length defined in characters / code points.
Find the character set used, and pass it explicitly to the length function.
<?php
mb_strlen($value, $charset);
iconv_strlen($value, $charset);
?>